[Tutor] class methods: using class vars as args?

Matthew Wood woodm1979 at gmail.com
Sun May 23 21:57:01 CEST 2010


Hey Alex,

What's happening is that you're still in "defining functions" mode on the
line
def doSomething(self, arg3=self.arg1):

self, which is really nothing more than a parameter being passed in (special
parameter, but a parameter none the less) hasn't been assigned a value yet.


Imagine this function definition:

def do_something(a, b, c=a+b):

The parameter instances haven't been instantiated yet.


Another way to look at it:

You haven't finished defining the class yet, so you can't access data
specific to an instance.


Not the most technical description, but it's certainly how I look at it.

--

I enjoy haiku
but sometimes they don't make sense;
refrigerator?


On Sun, May 23, 2010 at 1:40 PM, Alex Hall <mehgcap at gmail.com> 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!
>
>
> --
> Have a great day,
> Alex (msg sent from GMail website)
> mehgcap at gmail.com; http://www.facebook.com/mehgcap
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100523/967fd777/attachment.html>


More information about the Tutor mailing list