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

caccolangrifata caccolangrifata at gmail.com
Fri Jul 22 12:50:41 EDT 2011


On Jul 22, 5:43 pm, "bruno.desthuilli... at gmail.com"
<bruno.desthuilli... at gmail.com> wrote:
> On Jul 22, 1:12 pm, caccolangrifata <caccolangrif... at 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__"

as just said, Leaving aside the typos ...

> 2/ the argument name ('len') will shadow the builtin 'len' function
> within this function's scope.
>
> >                 self.__myvar = len

I have experience in java programming so using function calling
without () is foolish for me XD, but that a great suggestion

>
> 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.

I do not really already understand the mechanism of using private
public vars in python.

>
> >         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.

yep..when the range is known is better use for right.

>
> > 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.




More information about the Python-list mailing list