Attack a sacred Python Cow
benjamin.kaplan at case.edu
Sun Jul 27 05:07:24 CEST 2008
On Sat, Jul 26, 2008 at 10:23 PM, Marcus.CM <marcus at internetnowasp.net>wrote:
> 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. If you were writing an application and one of your classes
> adds the same variable to each of its member function you would do away with
> it too.
> 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. This would be similar to how the
> "this" keyword is used in C++.
> 2. Omit self from the parameter.
> class Abc :
> def DoSomething (a,b,c) :
> # class variable
> self.somevar = a
> self.someblar = b
> self.somec = c
> somevar = a * b # local variable
So, what happens in this case, using your Abc class from before?
def DoTheSameThing(abc, a, b, c) :
abc.somevar = a
abc.someblar = b
abc.somec = c
somevar = a * b
Abc.DoSomething = DoTheSameThing
Would methods defined this way never have access to the class instance?
> Russ P. wrote:
>> On Jul 26, 2:25 pm, Terry Reedy
>>> There is a lot of code you have not seen. Really. In informal code I
>>> use 's' and 'o' for 'self' and 'other'. I don't usually post such
>>> because it is not considered polite. So you have seen a biased sample
>>> of the universe.
>> You take the name down to a single letter. As I suggested in an
>> earlier post on this thread, why not take it down to zero letters? You
>> could if Python accepted something like
>> class Whatever:
>> def fun( , cat):
>> .cat = cat
>> This is even better than the single-character name, not only because
>> it is shorter, but also because there is no question that you are
>> referring to "self." No need to look back at the method signature to
>> verify that.
>> For those who don't like the way the empty first argument looks, maybe
>> something like this could be allowed:
>> def fun( ., cat):
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list