Attack a sacred Python Cow

Derek Martin code at pizzashack.org
Sun Jul 27 21:34:16 CEST 2008


On Sun, Jul 27, 2008 at 08:13:53AM +0000, Steven D'Aprano wrote:
> On Sun, 27 Jul 2008 10:23:06 +0800, Marcus.CM 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. 
> 
> You know, there are some programmers who haven't started with C++ or Java.

Indeed, I'm one of them.  In fact, I've never written even a single
program in either language (except maybe hello world or the
equivalent), and still, I have always thought that explicitly naming
the class instance variable in the parameter list of the object's
methods was a wart (albeit a very minor one) in Python.  It's a waste
of typing.

> > 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.
> 
> Would I?  How would I do that here?

You missed the point.  The variable "other" in your posted class is
not intended to always refer to the same *object*...  Whereas "self"
is and does, and that was what was meant.  In such a case, you'd
obviously convert the variable to a class property.

Regardless of how it's implementd, it's such a common idiom to use
self to refer to object instances within a class in Python that it
ought to be more automatic.  Personally, I kind of like the idea of
using @ and thinking of it more like an operator...  Kind of like
dereferencing a pointer, only with an implied pointer name.

    class foo:
	def __init__():
	    @.increment = 2

    	def bar(a)
	    return a + @.increment

I'm sure all the Pythonistas will hate this idea though... ;-)  To be
honest, it smacks a little of Perl's magic variables, which I actually
hate with a passion.  This is the only place in Python I'd consider
doing something like this.  

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20080727/84b94f77/attachment.sig>


More information about the Python-list mailing list