Attack a sacred Python Cow

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sun Jul 27 14:25:57 EDT 2008


Marcus.CM a écrit :
> Well after reading some of these posts on "sacred python cow" on the 
> "self" , i would generally feel that most programmers
> who started with C++/Java would find it odd. And its true, i agree 
> completely there should not be a need to put "self" into every single
> member function.

"member function" is C++. The way Python works is that you defines 
functions (inside or outside a class, that doesn't matter) that, when 
set as *class* attributes, are used as the *implementation* of the 
corresponding method.

I think it's very important to use appropriate terms to understand 
what's really going on here. As soon as you understand that what you 
"def"ine is a *not* a method, but a *function* (eventually) used as the 
*implementation* of a method, the necessary declaration of the target 
object (instance or class) in the function's signature just makes sense 
  - the usual way to make some object accessible to the body of a 
function is to pass this object as argument, isn't it ?

(snip)

> What could be done instead is :-
> 
> 1. python should hardcode the keyword "self". So whenever this keyword 
> is used, it would automatically implied that it is
> referring to a class scope variable.

Usually, 'self' is used for *instance* attributes, you know. "class 
attribute" are attributes of the class object itself (that is, shared by 
all instances of the class).



> This would be similar to how the 
> "this" keyword is used in C++.
> 
> 2. Omit self from the parameter.

Now how would it work for functions defined outside a class statement, 
but used as the implementation of a method ?

def toto(self):
    # dummy exemple code
    return self

class Tata(object):
    pass

Tata.tutu = toto

class Titi(object):
    tutu = toto

(snip)



More information about the Python-list mailing list