While everyone is saying what they want in Python :)

Michael Hudson mwh21 at cam.ac.uk
Sun Feb 4 13:26:51 EST 2001


warlock at eskimo.com (Jim Richardson) writes:

> On Sun, 04 Feb 2001 08:20:24 -0800, 
>  Daniel Klein, in the persona of <danielk at aracnet.com>,
>  brought forth the following words...:
> 
> >On Sun, 04 Feb 2001 14:19:04 +0000, Jay O'Connor <joconnor at cybermesa.com>
> >
> >That's cos Smalltalk returns 'self' by default when a there is no explicit
> >return value. This can be done in Python if your 'set' methods return 'self'
> >instead of 'None'. For example:
> 
> can someone explain to me what exactly "self" is for? I just don't
> get it.  (I am trying to learn this language, but this puzzles me.)
> Every time I think I get it, I find proof otherwise :)

Well, it's so methods can refer to the object they are invoked on.  If
they didn't have a way of doing that, there wouldn't be much point in
them being methods would there?

Consider:

class C:
    def __init__(i):
        m_j = i
        if m_j > 0:
           m_i = m_j
        else:
           m_i = -m_j
        
is "m_j" an instance or a local variable?

The point is that Python needs some way to distinguish instance &
local variables.  You could have some special attribute like:

class C:
    __attrs__ = ['m_a']
    def __init__(i):
        m_a = i

but this isn't very Pythonic (declarations aren't, in general).

It's just a difference from C++ and Java.  Get used to it.

If I haven't covered your reason for "not getting it", please feel
free to be more specific.

Cheers,
M.

-- 
  Now this is what I don't get.  Nobody said absolutely anything 
  bad about anything.  Yet it is always possible to just pull 
  random flames out of ones ass.
         -- http://www.advogato.org/person/vicious/diary.html?start=60



More information about the Python-list mailing list