-1

Hi,

I have been subclassing namedtuple to handle my custom data type:

fields=['Query', 'u1', 'Accession]

Old = namedtuple('Old', fields, verbose=False)

class New(Old):
    def __repr__(self):
        return 'Overwriting works nicely'

p=New('Hi',2,3)
print p
>>> 'Overwriting works nicely'

What I want to do is to add the behavior that some of my fields must be a certain type. For example, 'Query' must be a string. If the user does something like:

p.Query=50

I want it to raise an error. I tried overwriting the set_item attribute, but to no avail. Is there anyway to make the named tuple aware of what data type each field ought to have outright in the declaration? SOmething like:

fields=[str('Query'), int('u1'), int('Accession')]

Which doens't work. Thanks.

1
Contributor
1
Reply
2
Views
4 Years
Discussion Span
Last Post by hughesadam_87
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.