Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > While executing the class definition which object is referenced bythe first argument of the class method, Y r Object attributes not allowed asdefault arguments

Reply
Thread Tools

While executing the class definition which object is referenced bythe first argument of the class method, Y r Object attributes not allowed asdefault arguments

 
 
Krishna
Guest
Posts: n/a
 
      03-07-2008
>>> class Test(object):
.... def __init__(self):
.... self.a= 2
.... def func(self, k = self.a):
.... print k
....
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in Test
NameError: name 'self' is not defined
>>>


In the 'definition of the class', what would the first argument 'self'
in the methods evaluate to; when we have an object defined, it is
bound to the object reference, but what happens while the class
definition is executed, which I believe happens when the module
containing the class definition is imported

Thanks,
Kr
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-07-2008
En Thu, 06 Mar 2008 22:48:42 -0200, Krishna <>
escribi�:

>>>> class Test(object):

> ... def __init__(self):
> ... self.a= 2
> ... def func(self, k = self.a):
> ... print k
> ...
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "<stdin>", line 4, in Test
> NameError: name 'self' is not defined
>>>>

>
> In the 'definition of the class', what would the first argument 'self'
> in the methods evaluate to; when we have an object defined, it is
> bound to the object reference, but what happens while the class
> definition is executed, which I believe happens when the module
> containing the class definition is imported


Function default arguments are evaluated when the function is defined
(when the class is defined, in this case) so "self" itself has not a
value. Try this instead:

def func(self, k=None):
if k is None:
k = self.a
print k

If None is an allowed argument, use a special marker instead:

_marker=object()
....

def func(self, k=_marker):
if k is _marker:
k = self.a
...

--
Gabriel Genellina

 
Reply With Quote
 
 
 
 
Krishna
Guest
Posts: n/a
 
      03-07-2008
On Mar 6, 5:04 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> wrote:
> En Thu, 06 Mar 2008 22:48:42 -0200, Krishna <Krishna.0...@gmail.com>
> escribi�:
>
>
>
> >>>> class Test(object):

> > ... def __init__(self):
> > ... self.a= 2
> > ... def func(self, k = self.a):
> > ... print k
> > ...
> > Traceback (most recent call last):
> > File "<stdin>", line 1, in ?
> > File "<stdin>", line 4, in Test
> > NameError: name 'self' is not defined

>
> > In the 'definition of the class', what would the first argument 'self'
> > in the methods evaluate to; when we have an object defined, it is
> > bound to the object reference, but what happens while the class
> > definition is executed, which I believe happens when the module
> > containing the class definition is imported

>
> Function default arguments are evaluated when the function is defined
> (when the class is defined, in this case) so "self" itself has not a
> value. Try this instead:
>
> def func(self, k=None):
> if k is None:
> k = self.a
> print k
>
> If None is an allowed argument, use a special marker instead:
>
> _marker=object()
> ...
>
> def func(self, k=_marker):
> if k is _marker:
> k = self.a
> ...
>
> --
> Gabriel Genellina


Thanks for the reply. I am currently using the approach suggested by
you. But, I am more interested in knowing about the first argument
('self'), what does it hold to allow the evaluation of the method,
take the example you gave, 'self.a' as Rvalue inside the method, how
and why is this allowed, when the same 'self.a' is not allowed as the
default argument, considering the fact that I have already specified
'self' as first argument, only after whose evaluation, I believe would
the next statement (k = self.a, in def func(self, k = self.a) ) gets
evaluated

Thanks,
Krishna
 
Reply With Quote
 
castironpi@gmail.com
Guest
Posts: n/a
 
      03-07-2008
On Mar 7, 11:49Â*am, Krishna <Krishna.0...@gmail.com> wrote:
> On Mar 6, 5:04 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> wrote:
>
>
>
>
>
> > En Thu, 06 Mar 2008 22:48:42 -0200, Krishna <Krishna.0...@gmail.com>
> > escribi�:

>
> > >>>> class Test(object):
> > > ... Â* Â* def __init__(self):
> > > ... Â* Â* Â* Â* Â* Â* self.a= 2
> > > ... Â* Â* def func(self, k = self.a):
> > > ... Â* Â* Â* Â* Â* Â* print k
> > > ...
> > > Traceback (most recent call last):
> > > Â* File "<stdin>", line 1, in ?
> > > Â* File "<stdin>", line 4, in Test
> > > NameError: name 'self' is not defined

>
> > > In the 'definition of the class', what would the first argument 'self'
> > > in the methods evaluate to; when we have an object defined, it is
> > > bound to the object reference, but what happens while the class
> > > definition is executed, which I believe happens when the module
> > > containing the class definition is imported

>
> > Function default arguments are evaluated when the function is defined
> > (when the class is defined, in this case) so "self" itself has not a
> > value. Try this instead:

>
> > Â* Â* Â*def func(self, k=None):
> > Â* Â* Â* Â* Â*if k is None:
> > Â* Â* Â* Â* Â* Â* Â*k = self.a
> > Â* Â* Â* Â* Â*print k

>
> > If None is an allowed argument, use a special marker instead:

>
> > _marker=object()
> > ...

>
> > Â* Â* Â*def func(self, k=_marker):
> > Â* Â* Â* Â* Â*if k is _marker:
> > Â* Â* Â* Â* Â* Â* Â*k = self.a
> > Â* Â* Â* Â* Â*...

>
> > --
> > Gabriel Genellina

>
> Thanks for the reply. I am currently using the approach suggested by
> you. But, I am more interested in knowing about the first argument
> ('self'), what does it hold to allow the evaluation of the method,
> take the example you gave, 'self.a' as Rvalue inside the method, how
> and why is this allowed, when the same 'self.a' is not allowed as the
> default argument, considering the fact that I have already specified
> 'self' as first argument, only after whose evaluation, I believe would
> the next statement (k = self.a, in def func(self, k = self.a) ) gets
> evaluated
>
> Thanks,
> Krishna- Hide quoted text -
>
> - Show quoted text -


Is there enough information at that point in the statement to assign
to k as specified by this language?

No.

Does there exist a possible language in which there is?

Yes.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      03-07-2008
On Fri, 07 Mar 2008 09:49:58 -0800, Krishna wrote:

> I am more interested in knowing about the first argument ('self'), what
> does it hold to allow the evaluation of the method, take the example you
> gave, 'self.a' as Rvalue inside the method, how and why is this allowed,


"self" is treated as an argument like any other argument, except that
when you call a method on an instance Python automatically provides the
self for you.

Consider the following piece of code:

>>> class Parrot(object):

.... def squawk(self, n):
.... return "spam " * n
....
>>> instance = Parrot()
>>> instance.squawk(3)

'spam spam spam '
>>> Parrot.squawk(instance, 3)

'spam spam spam '


In the first call, I call the squawk() method from the instance, and
Python automatically fills in the self argument.

In the second call, I call the squawk() method from the class. Since the
class doesn't know what instance I'm using, I have to manually provide
the self argument.

But inside the method, self is just a name in a namespace, like any other
name. It's not special. You can do anything you like to it. You can even
re-assign to it, or delete it:

>>> class Spam(object):

.... def spam(self):
.... print "I am", self
.... self = "foo"
.... print "Now I am", self
....
>>> x = Spam()
>>> x.spam()

I am <__main__.Spam object at 0xb7f5192c>
Now I am foo
>>> x

<__main__.Spam object at 0xb7f5192c>



> when the same 'self.a' is not allowed as the default argument,


It isn't that self.a is "not allowed". Python doesn't contain any code
that says "if the default value contains "self", raise an error. You can
prove that for yourself:

>>> def foo(x=self):

.... return x
....
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'self' is not defined
>>>
>>> self = 2
>>> def foo(x=self):

.... return x
....
>>> foo()

2



> considering the fact that I have already specified 'self' as first
> argument, only after whose evaluation, I believe would the next
> statement (k = self.a, in def func(self, k = self.a) ) gets evaluated


You believe wrong. You've already been told that the function default
"k=self.a" is evaluated when the method is compiled, not at runtime.
Since "self" doesn't exist at compile time, it is an error.

There is no way to create a reference to a class instance before the
class is even defined.




--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why defining a constant in a method is not allowed but usingself.class.const_set is allowed? Iñaki Baz Castillo Ruby 13 05-01-2011 06:09 PM
passing arguments from a python program to other while executing itwith exec() or spawn() in LINUX gaurav kashyap Python 2 10-16-2008 08:16 AM
Member having only referenced attributes Massimo Burcheri C++ 1 07-12-2007 11:46 AM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM
Problems when destroy object which is referenced member objects Minkyu Kim Python 1 02-19-2004 03:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57