[Tutor] class methods: using class vars as args?
Mark Lawrence
breamoreboy at yahoo.co.uk
Thu May 27 22:21:09 CEST 2010
On 23/05/2010 20:40, Alex Hall wrote:
> Hello all,
> I know Python reasonably well, but I still run into basic questions
> which those over on the other python list request I post here instead.
> I figure this would be one of them:
> Why would this not work:
>
> class c(object):
> def __init__(self, arg1, arg2):
> self.arg1=arg1
> self.arg2=arg2
>
> def doSomething(self, arg3=self.arg1):
> ...
>
> The above results in an error that "name 'self' is not defined". Why
> can I not set the default values of a method's arguments to class vars
> like that? Thanks!
>
>
You've already had some explanations as to what happens, but what are
you trying to achieve? Why don't you forget about arg3 because it is
arg1, which must exist by creating an instance of class c, or you
wouldn't be able to call doSomething in the first place?
HTH.
Mark Lawrence
More information about the Tutor
mailing list