3 ms·
CoffeeScript feels for me like more flexible Python. Only thing I was missing until ES6 were generators. I simply love how I can wrap line from csv file in cus
by scotty79 11y ago
CoffeeScript feels for me like more flexible Python. Only thing I was missing until ES6 were generators.
I simply love how I can wrap line from csv file in custom CoffeeScript object just by declaring class like:
class Entry
constructor: ([@id, @description, @amount]) ->
and doing new Entry(line.split(','))
- task_queue 11y agoAn 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.