[Tutor] Methods that return instances of their own class?
alan.gauld at btinternet.com
Thu Oct 15 20:04:08 CEST 2009
"Che M" <pine508 at hotmail.com> wrote
> Since this is the tutor list, I'd like to ask some questions about
> structures used here that I haven't encountered before. I hope
> you'll excuse me asking
> class Payoffs(list):
> def __init__(self, value=None):
> This class is a list that has methods? That seems sort
> of unusual to me. Am I interpreting that right?
Its right and normal. Standard Python lists have methids try dir(list)
> How is this class called? With a list AND a value?
No, just a value, like any other class.
All that is happening is normal class definition where the
superclass is a built in type.
> does it mean to initialize a list (the third line?).
Its initialising the underlying list - the superclass - the python list
Although it may not work as expected, I suspect the OP may need
to do this via creating a __new__ method.
> here. But if value == None, was there any list to extend,
It means no value was provided so we are creating
a default one
> Why not just pass a list and to a class directly, and if
> not use a default list without having to use .extend()?
I'm not sure why extend is being used - I didn't look that
closely, but essentially the OP is doing what you suggest.
> def __repr__(self):
> What is the reason for using __repr__() here and also
> this |1 style? I have not seen this before.
Its the OPs prefered style of representing his list.
You can always override repr to present the object in a more
readable style - where readable is subjective.
Author of the Learn to Program web site
More information about the Tutor