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

2011-07-22 Thread caccolangrifata
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 want to use optional parameter, so i can use
myfoo = foo() or myfoo = foo(20)
and also
foo.foo2(20) or foo.foo2()

but in def foo2(self, len = self_myvar):
 ^
self is undefined, so:
How can I do this stuff?
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-22 Thread caccolangrifata
On Jul 22, 1:33 pm, Thomas Jollans  wrote:
> 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.

Yep! Leaving aside the typos, that's exactly I want to do.
Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-22 Thread caccolangrifata
On Jul 22, 5:43 pm, "bruno.desthuilli...@gmail.com"
 wrote:
> On Jul 22, 1:12 pm, caccolangrifata  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 :
>     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.

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-22 Thread caccolangrifata
On Jul 22, 6:54 pm, Karim  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
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-07-22 Thread caccolangrifata
Think that you can call you class as you want.

If you use CamelCase notation than you are pro programmer?

These are just conventions for better code reading and understanding,
if I wanna call mYCLasS() python don't report an error, so I think
it's useless discuss in that thread about that stuff.
-- 
http://mail.python.org/mailman/listinfo/python-list