7

I am writing a program to simulate a small physical system and have become more and more annoyed as I write things like this:

K = 0.5 * self.m * self.v**2

In the case above, the equation is short and pretty understandable, but I have situations in which there is so much self that the whole thing ends up looking like a mess. I am aware that python always requires self to refer to class members, but is there a way to to make the code not look like a mosaic of self's?

EDIT: I usually do things such as:

var = self.var

and keep on using var instead of self.var. Later I do:

self.var = var

but this seems really stupid. What would be the pythonic way to solve this problem?

pap42
  • 1,236
  • 1
  • 17
  • 28
  • 1
    If equations seem messy, you could maybe break them down and implement them using separate methods. – Jayanth Koushik Feb 16 '14 at 18:48
  • 3
    You could always change `self` to `s`, but others reading your code might hate you – mhlester Feb 16 '14 at 20:04
  • 4
    If you experience situations in which you are confronted to a messy bunch of ``self``s and if you are used to replace ``self.var`` with ``var`` (if so, are you sure to need an instance of a class ?), I suspect it is because your way of coding is perverted with some twisted algorithmic ideas. You must give us some examples of such situations to allow us to examine if you are justified to think that your algorithms are good and abundance of self is wrong instead of the contrary. – eyquem Feb 16 '14 at 20:14
  • 7
    the pythonic way would be to accept the self's... – Jörg Beyer Feb 16 '14 at 20:33
  • Indeed: you ARE doing it the pythonic way. `self.var` tells everyone where it comes from, and where it goes if you modify it. There are namespace tricks one can play with `globals()` and `locals()`, but while python makes them possible, they're not very "pythonic" in the sense of good practice... – alexis Feb 16 '14 at 21:38

3 Answers3

3

For messy parts I'd use Python modules and "module-level variables" instead of classes.

iljau
  • 2,151
  • 3
  • 22
  • 45
2

If all you want to do is save some keystrokes, you can always rename self to s:

class MyClass(object):
    def kinetic_energy(s): # use s instead of self for brevity
        return 0.5 * s.m * s.v**2

This saves you 3 characters per use of self. This goes against the standard convention, but nothing is stopping you from doing this. I would advice against doing this in general code, but it might be justified if it makes some very long formulas more readable. Do mention the unusual choice in a comment, in case anyone else has to read your code when you are long gone.

Bas Swinckels
  • 18,095
  • 3
  • 45
  • 62
0

I guess it's possible to use some black magic in Python and come up with a context manager, which will take an object and put all its attribute in the context's locals(), and assign it back to object in the __exit__ function.

Found this https://code.google.com/p/ouspg/wiki/AnonymousBlocksInPython which may help.

yegle
  • 5,795
  • 6
  • 39
  • 61