9 Replies - 2903 Views - Last Post: 22 October 2011 - 04:25 PM Rate Topic: -----

#1 stackoverflow  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 165
  • View blog
  • Posts: 545
  • Joined: 06-July 11

I wish Python would kill its self!

Posted 07 October 2011 - 09:46 AM

Ok-- a dramatic title.

However, there's one thing I can not stand with Python. The parameterization of self within class methods. I read some articles and blogs about "self" and why the methods are passed a reference to itself-- but I don't buy it.

Forgetting to pass self is probably one of the most common mistakes I make when coding a class and it is something that simply shouldn't happen. That, and forgetting to refer to a variable by "self.variable_name" within the class. Why can't it simply assume we are referring to self? Wouldn't it be simpler to refer to external or globals by using an "global.variable_name" or some other method? It seems much more "rare" to refer to an external. Referring to self is so common, explicitly stating self is quite a headache.

Anyway-- I just wanted to know how you felt about your self and if there is any secret reasons why Python has kept its explicit self.

Note:

I read this post about why removing self wouldn't work, but still many people seem to think removing self is a good idea (myself included).

Bruce Eckel posted on his blog about removing self.

Is This A Good Question/Topic? 4
  • +

Replies To: I wish Python would kill its self!

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4439
  • View blog
  • Posts: 12,310
  • Joined: 18-April 07

Re: I wish Python would kill its self!

Posted 07 October 2011 - 10:26 AM

I actually fall on the side of Bruce and I find Guido's arguments really just a set of special situations that really leverage the use of self. I mean you can change little things in a language and it will create all kinds of "interesting side effects".

The real debate here is if the change helps the core of the language and the goals behind the language. Not if it breaks edge cases created because of the original way of doing it.

I think it is a valid idea to make the calling and the method signature similar by making self implicit. C++ does this very well with 'this' and it saves typing, reduces confusion, makes even error messages nicer and will hopefully avoid programmers from making the mistake. After all, languages should strive for readability, simplicity and reduce programmer error.

Not sure why you would want to "poke" a method into a class when you could accomplish something similar with an interface or whatever. But making self a bit implicit on definition might be pretty nice and I don't see it hurting too much. Backwards compatibility can still be upheld. If self is defined, fine use it. Otherwise implicitly make self.

:)
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5942
  • View blog
  • Posts: 12,871
  • Joined: 16-October 07

Re: I wish Python would kill its self!

Posted 07 October 2011 - 10:35 AM

When I first ran into a language that was case sensitive, I cursed the hell out of it. Then you get used to it. Then you even come to like it and get annoyed at it's lack.

While I understand the frustration with the self thing, and find arguments why it's can't be done empty, I actually kind of like it. I like seeing "this" in Java, even though you usually don't need it. I always use "this" in Javascript, because the scoping is wonky to being with. The constant scope reminder is not entirely a bad thing.
Was This Post Helpful? 0
  • +
  • -

#4 stackoverflow  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 165
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Re: I wish Python would kill its self!

Posted 07 October 2011 - 10:52 AM

Hmm, I would have to disagree.

For instance, the use of white space in Python reduces the amount of code needed to demarcate code blocks. However, you could also argue languages that explicitly mark code blocks with braces are making it more "readable." That isn't the case though-- since white space clearly marks code blocks as well.

I also believe there can be a simplicity in an implied self rather than an explicit one. Firstly, we reduce the amount of code by quite a bit. I believe that jives with Python-- Python introduced white space to reduce the amount of code while remaining readable as well. It feels like the explicit self goes against what Python started out trying to do.

I would like to see an optional self.
Was This Post Helpful? 0
  • +
  • -

#5 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: I wish Python would kill its self!

Posted 07 October 2011 - 12:57 PM

Being entirely aesthetic, this is a hard topic to argue.

Personally, I like it. It makes sense to me that I'm passing an object, and that if I want to reference that object I need to assign it as part of the method call. I like this convention because it affords more understanding when doing class composition and introspection.

A simplified piece of code from a project I am currently working on:

class bsclient:
    ...
    def write(self, data):
        self.conn.send(self.serialize())
    def serialize(self): pass
...
def pickleSerialize(ojb):
    return pickle.dumps(obj)
...
myObj = bsclient()
myObj.serialize = pickleSerialize
myObj.write()



In the advanced case, I could composite multiple functions into a single, existing object or class. Could this be done with multiclassing? Sure, but as in the code above, I merely need to add the function I want; and I can do so without having to know what class/classes make up the parent. This is a major benefit. Also note; calling pickleSerialize() will execute perfectly in both the classed and classless call.

Regarding Martyr2's question about poking functions, I'm a maintainer for socksipy-branch, a library which boast the ability to take Python's standard library modules and swap out their socket module, allowing you to easily modify a program using urllib2 (for example) to connect via a proxy--without editing either the module code or the program code.

The problem from a backwards-compatibility perspective is that self is not a reserved word; as you can see in my example, the object reference could easily be named this, i, or chicken_soup for that matter. There would be no way to know that the first argument was a self-reference.
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4439
  • View blog
  • Posts: 12,310
  • Joined: 18-April 07

Re: I wish Python would kill its self!

Posted 07 October 2011 - 02:22 PM

Interesting project Motoma, I hadn't thought of doing something like that. Well maybe because most languages require you to just wrap the classes and then use the wrapper. Definitely interesting concept though.

As for the reserved word deal, going back to my original comments I would definitely make self a reserved word just like 'this' is. I guess what I am saying is that I would bring in the same thing 'this' does for C++ and put it in for Python.

:)

This post has been edited by Martyr2: 07 October 2011 - 02:24 PM

Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: I wish Python would kill its self!

Posted 08 October 2011 - 12:34 AM

python favors explicit over implicit. Self is an example of this. That is all.
Was This Post Helpful? 3
  • +
  • -

#8 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: I wish Python would kill its self!

Posted 08 October 2011 - 04:23 AM

I was basically going to say the same as atraub. And then quote the "Zen of Python".

It is a part of the language but I can't say I like it, but I don't dislike it either, it's Python and that's the way you do it. I guess every language has syntax where you say "I wish this was more like language X"
Was This Post Helpful? 0
  • +
  • -

#9 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: I wish Python would kill its self!

Posted 08 October 2011 - 07:09 AM

I'm not even bothered by self :P As mentioned by Simown and atraub, it's better to be explicit than implicit.

It's easier to explain self in Python than this in C++/Java to students not familiar with object oriented programming. In C++/Java, it seems like voodoo, seemingly coming out of no where, where as in Python, It's right there. :P

Quote

Why can't it simply assume we are referring to self? Wouldn't it be simpler to refer to external or globals by using an "global.variable_name" or some other method? It seems much more "rare" to refer to an external. Referring to self is so common, explicitly stating self is quite a headache.


Indeed, referring to self is quite common. But variables local to a method don't belong to the object, nor are they global.

public class Example {
   String name;

   public void method() {
      String name;

      name = "Where do I live?";
   }
}



Which variable is assigned the string assigned to? The answer to the question is irrelevant, what's relevant is that it had to be asked. In Python though...

class Example(object):
    def method(self):
        name = "Where do I live?"



There's no ambiguity. That's a local variable, without a doubt. Less wondering is a good thing :)

This post has been edited by Hiram: 08 October 2011 - 07:10 AM

Was This Post Helpful? 1
  • +
  • -

#10 yunusabd  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 104
  • Joined: 25-October 08

Re: I wish Python would kill its self!

Posted 22 October 2011 - 04:25 PM

If you are highly opposed to using this then you can write a PEP http://www.python.or.../peps/pep-0001/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1