On Jul 22, 1:12 pm, caccolangrifata <caccolangrif...@gmail.com> wrote:
Totally OT but others already answered the question... > class foo(object): class names should start with an uppercase letter: class Foo(object): > > __init__(self, len = 9): 1/ you want to add a "def" statement before "__init__" 2/ the argument name ('len') will shadow the builtin 'len' function within this function's scope. > self.__myvar = len There are very few reasons to invoke the __name_mangling mechanism. Canonically, implementation attributes (ie: not part of the API) are written with a *single* leading underscore. Also and FWIW, there's no need to "hide" public attributes and add dummy accessors in Python since you can turn a plain attribute into a computed one latter without breaking client code, so only use _implementation attributes if you really mean implementation. > def foo2(self, len = self_myvar): > while i < len: > dosomething Most of the time, this is spelled: for x in <somesquence>: do_something Note that range() can provide the required sequence. > I want to use optional parameter, so i can use > myfoo = foo() or myfoo = foo(20) > and also > foo.foo2(20) or foo.foo2() Note that default values for function params are only computed once, when the def statement is evaluated. This is a famous gotcha, specially if you use some mutable object as default value... Also, since neither the class nor - a fortiori - the instance exist when the def statement is evaluated, there's no way to make reference to the instance at this time. -- http://mail.python.org/mailman/listinfo/python-list