6 Replies - 931 Views - Last Post: 08 March 2016 - 09:25 AM Rate Topic: -----

#1 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 326
  • View blog
  • Posts: 1,105
  • Joined: 27-December 13

The four most used letters in Python

Posted 06 March 2016 - 11:35 AM

The four most used letters in Python

I admit that I am addicted to Python. I find it easy, quick, elegant and versatile.
One thing, however, often catches my eye and irritates my sense of rationality.
Four letters, that is.
'self.'
Writing a decent object oriented program, with a number of classes, each with attributes and methods; I end up writing 'self.' hundreds of times.
Looking down a page of Python code I often find that two thirds of the line start with 'self', and many has additional 'self's in the running code.
self, self, self....
self.attributes
self.method()
I know that it is possible to use other than self – any accepted variable can substitute 'self'. In each and every Python book I have ever read, it is strongly advised against, however. Don't do it. Verboten.

Now, sometimes a small Anarchist creeps into me.
”Why not?”, it says.
Is it really as bad as when Eve ate the apple from the tree of wisdom in the garden of Eden?
”Do it!”, it says.
”Try zubztitute 'self' with 'z'!”, it says.
z.attributes
z.method()
”zo zimple, - what are you waiting for”, it whisperz in my ear.

”No, no, no – I don't want” I say.
But is it true?

Imagine my life as Python programmer without those annoying, irritating, time- and space consuming 'self's.
Four little letters, that catches my attention repeatedly whenever looking at Python code.
Four little letters, used again and again by a whole Python community.
Four little letters, the most frequently used in Python.
Substituted with 'z'.

The Anarchist may win, - I'm afraid.

Is This A Good Question/Topic? 1
  • +

Replies To: The four most used letters in Python

#2 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10720
  • View blog
  • Posts: 18,353
  • Joined: 19-March 11

Re: The four most used letters in Python

Posted 06 March 2016 - 12:00 PM

Interesting dilemma, but not one that I can really sympathize with. For me, the single most important thing about the code that I write is that other people working on it - including ones that I have not yet met and might never meet - should be able to sit down and read the code easily. This means that sticking to conventions is very important to me. The saving of a few characters is not a convincing reason to obfuscate the code, in my opinion.
Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 6996
  • View blog
  • Posts: 14,635
  • Joined: 16-October 07

Re: The four most used letters in Python

Posted 07 March 2016 - 05:26 AM

For the sake of saving three letters?!?

Your gain: an aesthetic that only you may enjoy.

Your loss: clarity beyond measure. When going through a large Python program, self immediately tells you that you're working with an instance method. That is worth so much more than killing an "elf".

Even were the convention "instance" I would still use it, just to have the advantage of the convention.

There are many things you CAN do in languages. Names are a common place of contention; you can usually use what you like. A language will usually offer a list of acceptable conventions. Ignoring these conventions is willfully being a less competent user of that language.

For Python, you may review here: https://www.python.o.../peps/pep-0008/
Was This Post Helpful? 2
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10720
  • View blog
  • Posts: 18,353
  • Joined: 19-March 11

Re: The four most used letters in Python

Posted 07 March 2016 - 01:33 PM

View Postbaavgai, on 07 March 2016 - 07:26 AM, said:

That is worth so much more than killing an "elf".


"killing an 'elf'" would have been a great title for this topic...
Was This Post Helpful? 0
  • +
  • -

#5 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 326
  • View blog
  • Posts: 1,105
  • Joined: 27-December 13

Re: The four most used letters in Python

Posted 08 March 2016 - 05:29 AM

I am sure you understand that my little 'essay' is meant to have a humorous nerve.
But humor is only fun if it has a touch of seriousness!

Answering the question:

Quote

For the sake of saving three letters?!?

No, not for the sake of saving three letters, but maybe for saving three letters times hundred; or three letters times tens of thousands if we look at the Python community as a whole.

Is it realistic?
No.
But things may change.
IBM did not believe in PCs.
Many didn't understand the need for tablets.
You never know...

Until further: I'll exile the Anarchist.

Oh, one more thing: Thank you for responding. This is really the most important part; having discussions in a live community. Many more should participate.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 6996
  • View blog
  • Posts: 14,635
  • Joined: 16-October 07

Re: The four most used letters in Python

Posted 08 March 2016 - 06:54 AM

View PostDK3250, on 08 March 2016 - 07:29 AM, said:

No, not for the sake of saving three letters, but maybe for saving three letters times hundred;


Still three letters, not matter how often you type it. ;)

You can make a similar case for curly brace languages. Most such languages, C, C++, C#, Java, Javascript, etc, will allow you to drop the curlies if the code block is a single statement.

Thus:
if (foo) {
    print("bar");
}



May be written:
if (foo) 
    print("bar");



There are a lot of curlies in such languages and I just saved two! I could save so much more!

Of course, this:
if (foo) {
    print("bar");
    print("baz");
}



In completely not the same as:
if (foo) 
    print("bar");
    print("baz");



And that's a fun error to find. I suggest ALWAYS using curlies because saving the characters is simply not worth it.

Likewise, you can do your program entirely in globals. Ah, how nice not to pass variables around. The typing saved makes elf killing seem trivial! What's wrong with that? Trust me, it's wrong.

There are so many things that you can do in a programming language. It's up to you if you should. If you are the only one doing it, the answer is probably no.
Was This Post Helpful? 2
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10720
  • View blog
  • Posts: 18,353
  • Joined: 19-March 11

Re: The four most used letters in Python

Posted 08 March 2016 - 09:25 AM

This discussion reminds me of this old thread from atraub

On another level, this all really reminds me of how good we've got it in python. I mean, the python object system has its quirks, but looking at other languages I realize that it's actually a really nice blend of simplicity and power. True, there are some weird spooky things (method resolution order, for example) but for the most part you only get into weird and spooky when you are writing stuff that needs weird and spooky - python gets weird as you get weird, which is somehow appropriate.

The "self" issue really arises as a sensible consequence of a two good design decisions: the decision to use a simple reference instead of some special keyword to refer to "the object we're in right now", and the decision to strongly emphasize standards where needed. Yes, it means you run into a constraint in this one case, but the result of the constraint is a great good.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1