我有两个文件,都在同一个项目中(网络抓取框架的一部分)。 File1 处理 File2 生成的项目。在 File2 中,我有一个函数可以打印出有关进程的一些基本统计信息(已生成的项目数等)。我在 File1 中有计数,我想使用 File1 中的统计信息打印这些计数,但不确定如何执行此操作。看一下示例代码。
文件1:
class Class1(object):
def __init__(self):
self.stats = counter("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
文件2:(这就是我想做的)
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.
错误:
exceptions.AttributeError: type object 'Class1' has no attribute 'stats'
我知道这两个文件都在运行,因此“计数器”类的“统计”实例也在运行,因为其他方法在运行项目(这只是一个精简的示例。我在这里做错了什么?这可以做到吗?
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 = counter("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?
发布评论
评论(3)
这不起作用,因为您从未实例化
Class1
。__init__
在实例化Class1
时调用,因此设置了Class1.stats
。这里你有两个选择。
Class1
。Class1
中声明一个返回 count 属性的静态方法。This is not working because you never instantiate
Class1
.__init__
is called when theClass1
is instantiated soClass1.stats
is set.You have 2 options here.
Class1
in file 2 somehow.Class1
that returns the count property.在 File1 中,创建 Class1 的实例,并使用它来获取计数。
在file2中,使用创建的实例:
In File1, create an instance of Class1, and use that to get the count.
In file2, use the created instance:
你的术语有点混乱。 “两个文件都在运行,因此 'counter' 类的 'stats' 实例也在运行” -
stats
是 objects 的属性counter
类。如果您想要计算创建了多少个类实例,则应该使用类属性,它是绑定到您的类的属性,而不是它的实例。那么这可以像这样使用,
请注意,第二次
print foo
它已经更新了计数,但id
保持不变,那是因为count
是一个类属性,但id
是对象的属性,因此(使用此代码)bar
的创建不会影响foo
的 >id,但它会递增计数器.count
。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 thecounter
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.So then this can be used like so,
Note that the second time you
print foo
it has updated the count, but theid
remains the same, that is becausecount
is a class attribute, butid
is an attribute of the object, so (with this code) the creation ofbar
doesn't affect theid
offoo
, but it incrementsCounter.count
.