confused about self, why not a reserved word?

G

globalrev

class Foo(object):
def Hello(self):
print "hi"

object is purple, ie some sort of reserved word.

why is self in black(ie a normal word) when it has special powers.
replacing it with sel for example will cause an error when calling
Hello.
 
W

Wojciech Walczak

2008/5/5 said:
class Foo(object):
def Hello(self):
print "hi"

object is purple, ie some sort of reserved word.

why is self in black(ie a normal word) when it has special powers.
replacing it with sel for example will cause an error when calling
Hello.

Could you give an example of such an error?
 
M

Marc Christiansen

globalrev said:
class Foo(object):
def Hello(self):
print "hi"

object is purple, ie some sort of reserved word.

why is self in black(ie a normal word) when it has special powers.

Because `self` is just a name. Using `self` is a convention, but one can
use any name which is not reserved.
replacing it with sel for example will cause an error when calling
Hello.

Which error? I don't get one.

0:tolot:/tmp> python f.py
hi
0:tolot:/tmp> cat f.py
class Foo(object):
def Hello(sel):
print "hi"

Foo().Hello()
0:tolot:/tmp>

Marc
 
D

Diez B. Roggisch

globalrev said:
class Foo(object):
def Hello(self):
print "hi"

object is purple, ie some sort of reserved word.

It is not.
why is self in black(ie a normal word) when it has special powers.
replacing it with sel for example will cause an error when calling
Hello.

That must be some other problem. This is perfectly legal python code and
works:

class Foo(object):
def bar(imafancyobject):
print imafancyobject

f = Foo()
f.bar()

Diez
 
J

J. Cliff Dyer

class Foo(object):
def Hello(self):
print "hi"

object is purple, ie some sort of reserved word.

why is self in black(ie a normal word) when it has special powers.
replacing it with sel for example will cause an error when calling
Hello.

Wow. No it won't. sel is perfectly legal. I've seen s used on
occasion, and of course metaclass methods often use cls.

Direct ^C^V from my python interpreter:

$ python
Python 2.4.3 (#1, Dec 11 2006, 11:38:52)
[GCC 4.1.1 20061130 (Red Hat 4.1.1-43)] on linux2
Type "help", "copyright", "credits" or "license" for more information..... def Hello(sel):
.... print "hi"
....
Can you paste an example that breaks for you?
 
G

globalrev

sorry i noticed now a little error, i was playing around a lot and saw
that on the sel-example i had then called foo.hello() instead of
foo.Hello().


but it was an informing error now i egt it. was wondering about self
anyway so thanks for clearing it up.


but it still is a bit confusing that u can pass with any word.
wouldnt:


class Foo(object):
def Hello():
print "hi"


make more sense?

the class-thing seems inside out somehow in python. if im doing
foo.Hello() im already saying that im using the class foo because i
did foo=Foo() before and then still when calling Hello() i am
invisibly passing the class itself a s a parameter by default?
just seems backwards and weird.
 
D

dcoffin

the class-thing seems inside out somehow in python. if im doing
foo.Hello() im already saying that im using the class foo because i
did foo=Foo() before and then still when calling Hello() i am
invisibly passing the class itself a s a parameter by default?
just seems backwards and weird.

You're not passing a reference to the class, you're passing a
reference to the object. Any object-orientated programming language
will be passing methods references to an object; Python just makes
that explicit within the method definition. See http://www.python.org/doc/faq/general/#id35
 
B

Bruno Desthuilliers

globalrev a écrit :
sorry i noticed now a little error, i was playing around a lot and saw
that on the sel-example i had then called foo.hello() instead of
foo.Hello().


but it was an informing error now i egt it. was wondering about self
anyway so thanks for clearing it up.


but it still is a bit confusing that u can pass with any word.
wouldnt:


class Foo(object):
def Hello():
print "hi"


make more sense?

What would make sens in your example would be to either make hello a
staticmethod, since it doesn't use any reference to the current instance
nor it's class.

the class-thing seems inside out somehow in python.

Python's object model is indeed a bit pecular wrt/ most mainstream
OOPLs. But once you understand how it works, it happens to be mostly
well designed (IMHO) and really powerful.
if im doing
foo.Hello() im already saying that im using the class foo because i
did foo=Foo()

Nope. You are saying your using an instance of class Foo.
before and then still when calling Hello() i am
invisibly passing the class itself a s a parameter by default?

It's not the class that get passed (unless you use a classmethod, but
that's not the case here), but the instance. And you are not "invisibly"
passing it, you just pass it another way. In fact, foo.hello() is
equivalent to type(foo).hello(foo).
just seems backwards and weird.

Each and every OOPL needs to have a way to "inject" the instance a
method was called on into the method's local namespace. Python solved
this it's own way - by making 'methods' thin wrappers around
instance/function couples, these wrappers being in charge of calling the
function with the instance as first argument. From the implementation
POV, this allow to build methods on existing, more general features -
functions and the descriptor protocol - instead of having to
special-case them. From the programmer's POV, this allow to define
functions outside classes, then dynamically add them as 'methods' either
on a per-class or per-instance basis.
 
T

Terry Reedy

| sorry i noticed now a little error, i was playing around a lot and saw
| that on the sel-example i had then called foo.hello() instead of
| foo.Hello().

Lesson: whenever one inquires about an 'error', one should cut and paste
the error traceback along with a relevant snippet of code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top