7 ms·
An equivalent in Python is >>> Entry = namedtuple("Entry", "id description amount") >>> Entry(*line.split(',')) Entry(id='1', description='an entry', amount=
by task_queue 11y ago
An equivalent in Python is
>>> Entry = namedtuple("Entry", "id description amount")
>>> Entry(*line.split(','))
Entry(id='1', description='an entry', amount='99')
- scotty79 11y agoNeat, but can I add few methods to my Entry touple? Can it inherit from something else? Can something else inherit from Entry? Won't named touple have some of its own methods that my methods could have collided with?
- chrismorgan 11y agoYou can use a `namedtuple` class as a base class, e.g. like this: class Entry(namedtuple('Entry', ('id', 'description', 'amount')), SomethingElse): def more_methods(self): pass
- scotty79 11y agoThank you for this example. It's exactly the thing I had with CoffeeScript
- task_queue 11y ago>Neat, but can I add few methods to my Entry touple? Yes, if you subclass it. > Can something else inherit from Entry? Yes, it can be subclassed. >Won't named touple have some of its own methods that my methods could have collided with Yes and no, it has the "index" and "count" methods any tuple should have, which can easily be overridden by specifying them as attributes in the second argument to the contructor, through reassignment during initialization or method overriding.