Access class instance from one file in another file?
I have two files, both are in the same project (part of a web scraping framework). File1 processes items that are generated by File2. In File2 I have a function that prints out some basic stats about the processes (counts of how many items have been generated, etc). I have counts in File1 that I would like to print with the stats from File1 but am unsure of how to do that. Take a look at the example code.
FILE 1:
class Class1(object):
def __init__(self):
self.stats = co开发者_JAVA技巧unter("name") #This is the instance that I'd like to use in File2
self.stats.count = 10
class counter:
def __init__(self, name):
self.name = name
self.count = 0
def __string__(self):
message = self.name + self.count
return message
FILE 2: (this is what I'd like to do)
from project import file1 # this import returns no error
def stats():
print file1.Class1.stats # This is where I'm trying to get the instance created in Class1 of File2.
#print file1.Class1.stats.count # Furthermore, it would be nice if this worked too.
ERROR:
exceptions.AttributeError: type object 'Class1' has no attribute 'stats'
I know that both files are running, thus so does the 'stats' instance of the 'counter' class, because of other methods being printed out when running the project (this is just a stripped down example. What am I doing wrong here? Is this possible to do?
This is not working because you never instantiate Class1
.
__init__
is called when the Class1
is instantiated so Class1.stats
is set.
You have 2 options here.
- instantiate
Class1
in file 2 somehow. - declare a static method in
Class1
that returns the count property.
Your terminology is a little mixed up. "both files are running, thus so does the 'stats' instance of the 'counter' class" - stats
is a attribute of objects of the counter
class. If you want a count of how many instances of the class are created, you should use a class attribute which is something that is bound to your class, and not an instance of it.
class Counter(object):
count = 0
def __init__(self, name):
self.name = name
Counter.count += 1
self.id = Counter.count
def __repr__(self):
return '<Counter: %s (%d of %d)>' % (
self.name, self.id, Counter.count)
So then this can be used like so,
>>> foo = Counter('Foo')
>>> print foo
<Counter: Foo (1 of 1)>
>>> bar = Counter('Bar')
>>> print bar
<Counter: Bar (2 of 2)>
>>> print foo
<Counter: Foo (1 of 2)>
>>>
Note that the second time you print foo
it has updated the count, but the id
remains the same, that is because count
is a class attribute, but id
is an attribute of the object, so (with this code) the creation of bar
doesn't affect the id
of foo
, but it increments Counter.count
.
In File1, create an instance of Class1, and use that to get the count.
class Class1(object):
def __init__(self):
self.stats = counter()
self.stats.count = 10
class counter:
def __init__(self, name):
self.name = name
self.count = 0
def __string__(self):
message = self.name + self.count
return message
class_instance = Class1()
In file2, use the created instance:
from project import file1
def stats():
print file1.class_instance.stats
精彩评论