从一个文件访问另一个文件中的类实例?

发布于 2024-12-02 15:27:47 字数 1052 浏览 2 评论 0 原文

我有两个文件,都在同一个项目中(网络抓取框架的一部分)。 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笛声青案梦长安 2024-12-09 15:27:47

这不起作用,因为您从未实例化 Class1

__init__ 在实例化 Class1 时调用,因此设置了 Class1.stats

这里你有两个选择。

  1. 以某种方式实例化文件 2 中的 Class1
  2. Class1 中声明一个返回 count 属性的静态方法。

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.

  1. instantiate Class1 in file 2 somehow.
  2. declare a static method in Class1 that returns the count property.
秋千易 2024-12-09 15:27:47

在 File1 中,创建 Class1 的实例,并使用它来获取计数。

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()

在file2中,使用创建的实例:

from project import file1

def stats():
    print file1.class_instance.stats

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
紫罗兰の梦幻 2024-12-09 15:27:47

你的术语有点混乱。 “两个文件都在运行,因此 'counter' 类的 'stats' 实例也在运行” - statsobjects 的属性counter 类。如果您想要计算创建了多少个类实例,则应该使用类属性,它是绑定到您的类的属性,而不是它的实例。

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)

那么这可以像这样使用,

>>> 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)>
>>>

请注意,第二次 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文