naming conventions (WAS: A scoping question)
Steven Bethard
steven.bethard at gmail.com
Tue Dec 28 15:41:08 EST 2004
It's me wrote:
> #=============
> import file2
> global myBaseClass
> myBaseClass = file2.BaseClass()
> myBaseClass.AddChild(file2.NextClass())
> #=============
[snip]
> #=============
> global myBaseClass
> class BaseClass:
> def __init__(self):
> self.MyChilds = []
> ...
> def AddChild(NewChild):
> self.MyChilds.append(NewChild)
> ...
> class NextClass:
> def __init__(self):
> for eachChild in myBaseClass.MyChilds: # <- ERROR
> ...
> #=============
Also worth mentioning if you're just starting with Python. Python has
some official naming conventions:
http://www.python.org/peps/pep-0008.html
These are just recommendations of course, but if you have the option
(e.g. you're not constrained by style enforced by your employer), and
you'd like your code to look more like standard Python modules, you
might consider using these suggestions. This would make your code look
something like:
#=============
import file2
global my_base_class
my_base_class = file2.BaseClass()
my_base_class.add_child(file2.NextClass())
#=============
#=============
global my_base_class
class BaseClass:
def __init__(self):
self.my_childs = []
...
def add_child(new_child):
self.my_childs.append(new_child)
...
class NextClass:
def __init__(self):
for each_child in my_base_class.my_childs: # <- ERROR
...
#=============
Steve
More information about the Python-list
mailing list