Re: understaning "self"



On Feb 21, 2:34 pm, "Poppy" <znfmail-pythonl...@xxxxxxxxx> wrote:
I've been searching online to try get a better understanding of what "self"
does when I define this parameter in my class functions. All I'm finding is
debates on whether "self" has any value to the language but that doesn't
help me in my newbie question. So the code excerpt below is from "Beginning
Python" Norton, Samuel, Aitel, Foster-Johnson, Richardson, Diamon, Parker,
and Roberts.

What I think "self" is doing is limiting the function call to only function
in "this" class. So in the function below "has" calls self.has_various(), if
I had a function called "has_various" in my program or another included
class using "self" insures that the "has_various" below is the one used. Am
I correct in my understanding?

I'll try to explain with as simple an example as I can think of. First
of all, 'self' is just a conventional name, it isn't a keyword and has
no special meaning per se. You could just as easily write:

In [10]: class A(object):
....: def __init__(this, n):
....: this.n = n
....:
....: def f(me):
....: print me.n
....:
....:

In [11]: a = A(42)

In [12]: a.f()
42

The point is that when you write a statement such as

a.f()

the interpreter uses 'a' in two ways: first, to find which function
f() to call; second, as a parameter to f() itself. The conventional
'self' helps you remind that the first argument is going to be the
instance of the class on which the function f() is going to be called.

You can actually separate the two uses above with the following
equivalent call:

In [13]: A.f(a)
42

Here you tell the interpreter which function f() to call by specifying
its class, A, and you pass it 'a' explicitly as an argument.

One could argue that the C++/Java/C# approach where you don't specify
the current instance as an argument, but can optionally use the 'this'
keyword to refer to it is more convenient; however the explicit 'self'
makes it possible for free functions and class methods to work in
exactly the same way. This in turn makes the language more consistent
and makes some more advanced, very effective techniques possible.

Hope this helps.

Cheers,
Nicola Musatti
.



Relevant Pages

  • Re: Why Ive returned
    ... It was a parody of an absurd idea. ... not have the C language required to major at Kernighan's Princeton, ... Kernighan and John Hennessy, the RISC computer architect, rejected the ... the firmware interpreter for 1401 code on the old IBM 360 mainframe, ...
    (comp.programming)
  • Re: Question on LSP
    ... not have to be explicit attributes, ... Since objects in memory usually don't move, the language can largely hide the identity mapping. ... My issue here is that whatever semantic meta model the language uses, there must be some way for the developer to unambiguously express the OOA/D is-a semantics for some problem space entity. ... Note that the language allows us to use a name like 'T' on the reference as a mnemonic so that the developer can keep track of what is happening with the indirection. ...
    (comp.object)
  • Re: Mathematical ASL?
    ... You can imagine when I was interpreting for computer science classes, and the professor said "UNIX", and my non-computer-literate interpreter colleague signed "eunuchs". ... Teachers may need to make clear that they are using word in a technical sense for both their hearing and deaf students. ... Sign language can represent italics or "quotes" during conversation in ways spoken langauge cannot, ... If you say "the difference between a series and a sequence is X", and the same sign is used for both series and sequence (as they are in non-technical ASL), the interpreter will easily be able to create that distinction for the students whether or not s/he knows the math signs, and maintain the distinction going forward. ...
    (sci.math)
  • Re: How to name variables in a program?
    ... So its an explicit encoding, ... We are also trained to read natural language as well. ... fact even beyond conventions it leads people to write abbreviations on ... > making the whole source code much harder to read. ...
    (comp.programming)
  • Re: OFF-TOPIC:: Why Lisp is not my favorite programming language
    ... The interpreter would be written in C. ... It is always possible to beat any interpreted language for speed using ... >at me for calling lisp a parsing language... ... I would write a parser to do so. ...
    (comp.lang.python)