Too many ‘self’ in python.That’s a big flaw in this language.”

June 28th, 2007 § 6 comments

The title is of course not my cre­ation — it is another thread on comp.lang.python (seri­ous, you should stop by, I lurk a lot and read posts from that xiu fellow).

In this par­tic­u­lar thread:

I’m cur­rently using Python. I find that a instance vari­able must
con­fined with self,
for exam­ple: [snip] That’s a big incon­ve­nience in cod­ing ‚espe­cially when you have lot of vari­able If you method need 10 vari­ables ‚you have to type “self” for 10 times and that also makes your vari­able longer. From My point,I think this only help python inter­preter to deside where to look for. Is there any­one know’s how to make the inter­preter find instance name space first?
Or any way to make programmer’s life easier?

That’s the orig­i­nal post. With­out delv­ing too deep into the nuances of his cri­tique — I like explicit self. As Bjorn wisely points out “Explicit is bet­ter than implicit.

I really don’t under­stand the near vis­ceral dis­like of “self.” some peo­ple have. I mean, “self” you’re pass­ing in, call­ing, ref­er­ence your local, “self” scope/object/underpants.

I mean, I really like self, and I find myself miss­ing it learn­ing Java-Fu.

  • http://www.dougma.com/ Doug Napoleone

    I agree. There was a great post on the mail­ing list at one point I think called ‘When self is not self’ which dived into all these cor­ner cases which would melt your mind if you didn’t have self. can’t find it right now for some rea­son. (Dealt with yield, gen­er­a­tor classes, descrip­tors, prop­er­ties, prop­er­ties on meta classes, etc).

  • http://www.pirnat.com Mike Pir­nat

    I’ve really come to love the explicit self, to the point that lan­guages that don’t have it just feel creepy and wrong to me. Maybe that means I’ve been focused on Python for too long? Or maybe it means that Python is the best fit for me that I’ve found so far.

  • http://dado1945.storas.lt dado1945

    I agree with you com­pletely. “Self” is almost no dif­fer­ent from “m_” ;)

  • andy

    I don’t really like “self” (too much typ­ing) so instead of “self” I just write “m”. That way, mem­ber val­ues look like “m.value”, which is a lot like the m_value nota­tion that peo­ple use in other languages.

  • http://www.hardcoded.net Vir­gil Dupras

    When I started python, I was com­ing from object pas­cal. I dis­liked self a lot. In object pas­cal, I liked using the “with” state­ment (no, it’s not like the cur­rent “with” in python).

    Today, you’d have to pry explicit self from my cold dead hands :)

    This guy only need to time to embrace python and stop try­ing to emu­late his old lan­guage behav­ior in python.

  • rgz

    Explicit self is good spe­cially if you make it a point of nam­ing your mem­ber fields con­sis­tently. I code in VB.NET by day, and I always find that I want to use the same name as:

    pri­vate field
    pub­lic prop­erty
    method para­me­ter
    class name

    On top of that VB.NET is case insen­si­tive, I always end up with a syn­tax like

    pri­vate _field # car­ried over from Python! i did’t knew of the m_ con­ven­tion when I started in VB.NET
    pub­lic Prop­erty # looks mat­ter in API design
    method(pParameter) # So method para­me­ters don’t shadow prop­er­ties, f*ck implicit self!
    uClass­Name # *u*ser class, maybe it is too ugly

    VB.NET allows using “Me” (like C++‘s “this”) when there is ambi­gu­ity, so I can say Me.Property,. That could let me drop the u from the class name and use the same con­ven­tion for fileds and para­me­ters, that way i can say:

    Me._field
    Me.Property
    method(_parameter)
    ClassName

    But in prac­tice is easy to for­get the “Me.” because the com­piler doesn’t enforce it and I haven’t found a way to make it com­plain when there is shad­ow­ing or ambi­gu­ity in local names. I wish VB.NET enforced “Me.”

    On the other hand while I like explicit self and I like “self” bet­ter than “Me” or “this”, i wish self was a key­word, with sim­i­lar seman­tics than javascrip’s “this”, that is, to refer to the out­ter name­space, wouldn’t that be sweet?

What's this?

You are currently reading Too many ‘self’ in python.That’s a big flaw in this language.” at jessenoller.com.

meta