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'

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:


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.

This can be deleted, sorry for the haste.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.