[Tutor] Problems with references
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Tue Jun 1 18:32:53 EDT 2004
On Tue, 1 Jun 2004, Martin Hjort Eriksen wrote:
> This is a question on how references work with Python.
>
> I have several objects, which use the same class definition. Within this
> class definition, there is an attribute of the type dictionary. During
> the run of the application, this dictionary, will be slowly filled up
> with references to other objects.
Hi Martin,
Let's look at two possible implementations of a class with an attribute:
###
class FirstExample:
names = []
def __init__(self):
pass
def add(self, name):
self.names.append(name)
def report(self):
for n in names:
print n
class SecondExample:
def __init__(self):
self.names = []
def add(self, name):
self.names.append(name)
def report(self):
for n in names:
print n
###
Does your class definition resemble either one of these?
The difference between the two here is that the FirstExample uses a shared
"names" class attribute. The SecondExample uses an instance-specific
"names" attribute that's created anew for each instance.
My guess is that the FirstExample's approach fits your problem
description, but then, I guess wildly. *grin*
Please tell us a few more details on your class. A small, simplified
example of the problem with source code will help us a lot, since then we
can duplicate the same problem.
Good luck to you!
More information about the Tutor
mailing list