Search code examples
pythonnaming-conventionsnotation

Python Notation?


I've just started using Python and I was thinking about which notation I should use. I've read the PEP 8 guide about notation for Python and I agree with most stuff there except function names (which I prefer in mixedCase style).

In C++ I use a modified version of the Hungarian notation where I don't include information about type but only about the scope of a variable (for instance lVariable for a local variable and mVariable for a member variable of a class, g for global, s for static, in for a function's input and out for a function's output.)

I don't know if this notation style has a name but I was wondering whether it's a good idea not to use such a notation style in Python. I am not extremely familiar with Python so you guys/gals might see issues that I can't imagine yet.

I'm also interested to see what you think of it in general :) Some people might say it makes the code less readable, but I've become used to it and code written without these labels is the code that is less readable for me.


Solution

  • (Almost every Python programmer will say it makes the code less readable, but I've become used to it and code written without these labels is the code that is less readable for me)

    FTFY.

    Seriously though, it will help you but confuse and annoy other Python programmers that try to read your code.

    This also isn't as necessary because of how Python itself works. For example you would never need your "mVariable" form because it's obvious in Python:

    class Example(object):
        def__init__(self):
            self.my_member_var = "Hello"
    
        def sample(self):
            print self.my_member_var
    
    e = Example()
    e.sample()
    print e.my_member_var
    

    No matter how you access a member variable (using self.foo or myinstance.foo) it's always clear that it's a member.

    The other cases might not be so painfully obvious, but if your code isn't simple enough that a reader can keep in mind "the 'names' variable is a parameter" while reading a function you're probably doing something wrong.