Use self.vars in class.method(parameters, self.vars)

caccolangrifata caccolangrifata at gmail.com
Fri Jul 22 12:59:34 EDT 2011


On Jul 22, 6:54 pm, Karim <karim.liat... at free.fr> wrote:
> You're right. Sure the method header is evaluated first I usually not
> fall in this trap when default is a list but a singleton one with the same
> id.
> I answered too fast, I did not understand if he forget the dot or what.
> And the double '_' in init was strange because he uses only one '_' after.
>
> Thanks to take time to point that.
>
> Regards
> Karim
>
> On 07/22/2011 02:06 PM, Thomas Jollans wrote:
>
>
>
>
>
>
>
> > On 22/07/11 13:32, Karim wrote:
> >> I think you did a typo
>
> >> it is
>
> >> def foo2(self, len = self._myvar):
> >>         while i<   len:
> >>       dosomething
>
> > That, of course, won't work: the default argument (in this case:
> > "self._myvar") is looked up when the function is created, and stored
> > with the function. "self" does not exist at that point. (or, if it does,
> > it's the wrong "self")

class foo(object):

        def __init__(self, len = 9):
                self.__myvar = len

        def foo2(self, len = self.__myvar):
                while i < len:
                        do_something

that the initial code, "." and "_" forgot



More information about the Python-list mailing list