The need to put "self" in every method

F

Fernando M.

Hi,

i was just wondering about the need to put "self" as the first
parameter in every method a class has because, if it's always needed,
why the obligation to write it? couldn't it be implicit?

Or is it a special reason for this being this way?

Thanks.
 
S

Steve Holden

Fernando said:
Hi,

i was just wondering about the need to put "self" as the first
parameter in every method a class has because, if it's always needed,
why the obligation to write it? couldn't it be implicit?

Or is it a special reason for this being this way?

Thanks.
The reason can be found in the output from the python statement "import
this". Explicit is better than implicit.

regards
Steve
 
J

John Roth

Fernando M. said:
Hi,

i was just wondering about the need to put "self" as the first
parameter in every method a class has because, if it's always needed,
why the obligation to write it? couldn't it be implicit?

Or is it a special reason for this being this way?

There are two different issues.

1. If self was made a keyword, there would be no need for
it in the method header. However, that would be a major
incompabible change affecting almost every script in existance.
Also some people use other words than self. (It would,
by the way, result in a performance improvement.)

2. There's been some talk of making the '.' operator allow an
implied instance. I'm not sure what the status of this is.

These two are actually separate issues; either could be
done without the other.

John Roth
 
S

Steven Bethard

Fernando said:
i was just wondering about the need to put "self" as the first
parameter in every method a class has because, if it's always needed,
why the obligation to write it? couldn't it be implicit?

py> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
....


STeVe
 
S

Simon Brunning

i was just wondering about the need to put "self" as the first
parameter in every method a class has because, if it's always needed,
why the obligation to write it?

It doesn't need to be 'self'. You could use 'this', or 's', or whatever.

Of course, if you *don't* use 'self', you should expect an angly mob
with pitchforks and torches outside your castle.
 
E

Erik Max Francis

Fernando said:
i was just wondering about the need to put "self" as the first
parameter in every method a class has because, if it's always needed,
why the obligation to write it? couldn't it be implicit?

Or is it a special reason for this being this way?

Because it's not always needed. See staticmethod or classmethod.
 
R

Roy Smith

Fernando M. said:
i was just wondering about the need to put "self" as the first
parameter in every method a class has because, if it's always needed,
why the obligation to write it? couldn't it be implicit?

Didn't this exact question get asked just a few days ago?

Anyway, the short answer is:

1) Yes, it's always needed.
2) It's part of the Python design philosophy that "explicit is better
than implicit".

Note that in languages like C++ where using "this->" is optional,
people invent their own conventions for keeping local and instance
variables distinct, like prepending m_ to member names (except that
different people do it different ways, so it's more confusing).
 
S

Skip Montanaro

Simon> Of course, if you *don't* use 'self', you should expect an angly
Simon> mob with pitchforks and torches outside your castle.

I take it an "angly mob" is a large group of stick figures? <wink>

Skip
 
P

Piet van Oostrum

Fernando M. said:
FM> Hi,
FM> i was just wondering about the need to put "self" as the first
FM> parameter in every method a class has because, if it's always needed,
FM> why the obligation to write it? couldn't it be implicit?
FM> Or is it a special reason for this being this way?

There is.
Inside a method there are 3 kinds of identifiers:
- local ones e.g. parameters and local variables
- global ones (actually module-level)
- instance variables and methods

Because Python has no declarations there must be a different way to
indicate in which category an identifier falls. For globals it is done with
the 'global' keyword (which actually is a declaration), for instance
variables the dot notation (object.name) is used and the rest is local.
Therefore every instance variable or instance method must be used with the
dot notation, including the ones that belong to the object `itself'. Python
has chosen that you can use any identifier to indicate the instance, and
then obviously you must name it somewhere. It could have chosen to use a
fixed name, like 'this' in Java or C++. It could even have chosen to use a
keyword 'local' to indicate local ones and let instance ones be the
default. But if instance variable would be implicit, local ones should have
been explicit.
 
J

John Machin

Simon said:
It doesn't need to be 'self'. You could use 'this', or 's', or whatever.

Of course, if you *don't* use 'self', you should expect an angly mob
with pitchforks and torches outside your castle.

Wouldn't an angly mob be carrying fishing rods?
 
J

John Machin

Skip said:
Simon> Of course, if you *don't* use 'self', you should expect an angly
Simon> mob with pitchforks and torches outside your castle.

I take it an "angly mob" is a large group of stick figures? <wink>

Skip

Yep -- straw men.
 
A

Aahz

[posted & e-mailed]

There is.
Inside a method there are 3 kinds of identifiers:
- local ones e.g. parameters and local variables
- global ones (actually module-level)
- instance variables and methods

Because Python has no declarations there must be a different way to
indicate in which category an identifier falls. For globals it is done with
the 'global' keyword (which actually is a declaration), for instance
variables the dot notation (object.name) is used and the rest is local.
Therefore every instance variable or instance method must be used with the
dot notation, including the ones that belong to the object `itself'. Python
has chosen that you can use any identifier to indicate the instance, and
then obviously you must name it somewhere. It could have chosen to use a
fixed name, like 'this' in Java or C++. It could even have chosen to use a
keyword 'local' to indicate local ones and let instance ones be the
default. But if instance variable would be implicit, local ones should have
been explicit.

Any objection to swiping this for the FAQ? (Probably with some minor
edits.)
 
T

Terry Hancock

Note that in languages like C++ where using "this->" is optional,
people invent their own conventions for keeping local and instance
variables distinct, like prepending m_ to member names (except that
different people do it different ways, so it's more confusing).

I would call this the "real" answer. I am aesthetically annoyed by
code like:

Post.postRead()

or even

Post.read_post()

The dot is there for a reason -- it qualifies the variable with the
class it applies to. What is the point of qualifying it AGAIN in the
method name? When I notice this in my own code, I reduce such
stuff to

Post.read()

IMHO, redundancy interferes with clarity, and one should use
the semantics of the language to describe the semantics of the
program --- that should make it easier for both Humans and
machines to understand the relationships in the code.

Using "self" is just like this, as indicated above: it replaces weird
naming conventions with a much more general one.

I've only ever replaced "self" with a different name once, and
that was when I was writing a vector math module -- I wanted
a more compact symbolic style, so I used "a":

def __add__(a,b):
return Vector((a.x+b.x), (a.y+b.y), (a.z+b.z))

or something like that. I still have twinges of guilt about it,
though, and I had to write a long note in the comments, apologizing
and rationalizing a lot. ;-)

I must say though, that as a newbie, I found this a lot easier
to get my head around than learning all the implicit variables
that Javascript introduces (e.g. "this", "prototype", etc).
 
M

Max M

Terry said:
def __add__(a,b):
return Vector((a.x+b.x), (a.y+b.y), (a.z+b.z))

or something like that. I still have twinges of guilt about it,
though, and I had to write a long note in the comments, apologizing
and rationalizing a lot. ;-)

Assigning self to a could have made it obvious:

def __add__(self, b):
a = self
return Vector((a.x+b.x), (a.y+b.y), (a.z+b.z))


--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top