Naming Conventions for Class Methods and Attributes
attr, class, methods, python
Solution
Methods should generally have a verb in them, eg `print_this`, `calculate_that`, `save`, because they perform some action.
Attributes and properties will just be a bare noun, eg `name`, `status`, `count` as they just contain a value or, in the case of a property, the underlying function should calculate and return a value, without other side-effects.
Your question is vague though:
is it about naming conventions in Python? For this you should refer to the famous PEP8 document
is it about how to distinguish between methods and attributes by looking at their names? for this see my rule of thumb above
is it about how to distinguish between methods and attributes programmatically? In this case you can use the `inspect` module to provide a definitive answer, for example:
import inspect
for attr in dir(a):
if inspect.ismethod(getattr(a, attr)):
print '%s is a method' % attr
else:
print '%s is an attribute or property' % attr
Problem
What would be a best practice to name the class attributes and its methods (functions). ``` class Base(): def __init__(self): self.name = 'My Name' def print_this(self): print "Hello" ``` Create an instance of the class: ``` a = Base() ``` Get an idea what methods and attributes available in this class: ``` print dir(a) ``` Getting this output: ``` ['__doc__', '__init__', '__module__', 'name', 'print_this'] ``` From reading the names in dir() output it is impossible to see where the variables are (class attributes) and where the methods (class functions). What naming conventions to use to differentiate the variables from functions (or attrs from methods)?