We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,459 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Helped with type declaration in named tuple

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
7 Hours
Discussion Span
10 Months Ago
Last Updated
2
Views
Question
Answered
hughesadam_87
Posting Whiz in Training
274 posts since May 2009
Reputation Points: 67
Solved Threads: 12
Skill Endorsements: 1
Question Self-Answered as of 10 Months Ago

This can be deleted, sorry for the haste.

hughesadam_87
Posting Whiz in Training
274 posts since May 2009
Reputation Points: 67
Solved Threads: 12
Skill Endorsements: 1

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.3154 seconds using 2.66MB