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

Thomas Jollans t at jollybox.de
Fri Jul 22 13:33:00 CEST 2011


On 22/07/11 13:12, caccolangrifata wrote:
> I'm very very new with python, and I have some experience with java
> programming, so probably you guys will notice.
> Anyway this is my question:
> I'd like to use class scope vars in method parameter, something like
> that
> 
> class foo(object):
> 
> 	__init__(self, len = 9):
> 		self.__myvar = len
> 
> 	def foo2(self, len = self_myvar):
> 		while i < len:
> 			dosomething
> 

I think what you want to do is this:

class foo (object):
    def __init__(self, len=9):
        self._len = len
    def foo2(self, len=None):
        if len is None:
            len = self._len
        # ...

Default arguments are for when you want to use exactly the same object
each time the function/method is called. If you the object you want to
use depends on something, you can use this arg=None idiom.



More information about the Python-list mailing list