views:

268

answers:

3

In python, there is no way to differentiate between arguments, local variables, and global variables. The easy way to do so might be have some coding convention such as

  • Global variables start with _ and capital letter
  • arguments end with with _
_Gvariable = 10
def hello(x_, y_):
   z = x_ + y_

Is this a Pythonian way to go? I mean, is there well established/agreed coding-standards to differentiate them in python?

=== ADDED ===

I just want to discriminate between arguments and local variables. As arguments are given from outside, and more like a ROM in the sense that it is not assumed to be read only. C++ provides the const keyword to prevent the arguments from changing, but not for python. I thought appending _ can be one of a way to mimic this feature in python.

+3  A: 

I would do all your python programming according to PEP 8 guidelines. Anyone who has to read your code will thank you for it.

http://www.python.org/dev/peps/pep-0008/

Why is there a need to distinguish between arguments and local variables, since one is merely a subset of the other. You can use locals(), globals(), and vars() to view scope if you are having local-global issues. The inspect module can help with that, too. And if possible, avoid using global variables as much as possible.

Brendan Abel
+2  A: 

That's absolutly awful. There is no reason whatsoever to use a special naming scheme for global and local objects. Also you should avoid having global objects unless they are functions, classes or constants.

Names for constants should be uppercase and seperated with underscores LIKE_THIS, class names look LikeThis and functions and method names should look like any other name. Names for objects which are implementation specific, can be changed/removed at any time or can not be relied on for any other good reasons should be prefixed with an underscore.

You should also read the Python styleguide PEP 8 which covers these and more style-related rules you should follow as long as it doesn't make your code less readable. Most Python projects follow this or at least a compatible version of this style guide.

DasIch
+2  A: 

It is usually obvious in python which variables are local and which are global, since to modify a global variable you have to declare it using the the global keyword at the start of a function. However I sometimes add a global declaration even if it is not necessary for python to compile it, in order to emphasize that an object is global - e.g. modifying a mutable global data-structure.

Arguments should be obvious because they are in the function declaration.

As others have said constants should be in UPPER_CASE_WITH_UNDERSCORES, which is a convention shared by many languages.

If you find that you are having trouble keeping track of which are global, local and parameter variables I suggest that the problem may be your functions are too long and doing too much. Functions & methods should be short and do exactly one thing. I start to get the refactoring itch if my functions go over about 10-20 lines of code.

I recommend reading the book Clean Code by Robert Martin. The examples are in Java, but the principles apply to all languages.

Dave Kirby