简体   繁体   English

在编写的类上而不是在Python中的对象上调用方法

[英]Calling a method on the class it is written in and not on an object in Python

I'm very new to python I apologize if this is a stupid question but so far I haven't been able to google it successfully. 我对python很陌生,如果这是一个愚蠢的问题,我深表歉意,但到目前为止,我还没有能够成功地对它进行谷歌搜索。

I'm making a class called Cookie and it has methods that return the number of cookies created and also resets them. 我正在创建一个名为Cookie的类,它具有一些方法,这些方法返回创建的cookie的数量并重置它们。 What I am having issues with is in the tests it calls the classes methods on the actual class and not an object created from the class. 我遇到的问题是在测试中,它在实际类上调用类方法,而不是从类创建的对象。 IE: IE浏览器:

instead of: 代替:

c = Cookie()
c.count()

they use in the tests: 他们在测试中使用:

Cookie.count()

when count is a method in the cookie class but cookie is not an object. 当count是cookie类中的一种方法,但cookie不是对象时。 So far I have not had success calling methods on the class that they are in, just on objects created by the class. 到目前为止,我还没有成功地在它们所在的类上调用方法,仅在类创建的对象上成功调用了方法。 I've always thought of classes as templates to create objects. 我一直认为类是创建对象的模板。 Can anyone explain this? 谁能解释一下? Thank you. 谢谢。

What I Have 我有的

Tests 测验

This code keeps a counter on the class object and increments it each time the class is instantiated. 该代码在类对象上保留一个计数器,并在每次实例化该类时对其进行递增。 Class methods get a reference to the class object so they can find num_cookies . 类方法获取对类对象的引用,以便它们可以找到num_cookies They are also callable through the self object in class instances, so you can get to them multiple ways. 它们也可以通过类实例中的self对象来调用,因此您可以通过多种方式获得它们。

class Cookie:

    num_cookies = 0

    @classmethod
    def count(cls):
        return cls.num_cookies

    @classmethod
    def add_cookie(cls):
        cls.num_cookies += 1

    def __init__(self):
        self.add_cookie()

print(Cookie.count())
print(Cookie().count())

Ok, after taking a look at the code, which I will past here: 好吧,在看完代码之后,我将在这里进行介绍:

class Cookie(object):
    global count
    count = 0

    def __init__(self, count = 0):
        self.count = count
        self.count += 1

    def count():
        print(count)

    def reset_counter():
        count = 0
        return count

You have a number of problems. 你有很多问题。

One, you're declaring count as a global variable, inside the body of the class object. 第一,您要在类对象的主体内部将count声明为全局变量。

Further along, you have a function called count . 更进一步,您有一个称为count的函数。

This is going to lead to some serious problems. 这将导致一些严重的问题。

Watch: 看:

In [8]: Cookie
Out[8]: __main__.Cookie

In [9]: count
Out[9]: <function __main__.count>

In [10]: 

I think what you want to go for is keep track of how often an object is instantiated. 我认为您想要做的是跟踪对象实例化的频率。

To achieve that, you don't need the global, it will just confuse things. 为此,您不需要全局,它只会使事情变得混乱。

In [18]: class Cookie(object):
    ...:     counter = 0
    ...: 
    ...:     def __init__(self):
    ...:         Cookie.counter += 1
    ...: 
    ...:     @classmethod
    ...:     def count(cls):
    ...:         print(Cookie.counter)

    ...:     @classmethod
    ...:     def reset_counter(cls):
    ...:         Cookie.counter = 0
    ...:         return Cookie.counter
    ...:     

In [19]: c=Cookie()

In [20]: Cookie.count()
1

In [21]: c=Cookie()

In [22]: Cookie.count()
2

You also need to be a bit more careful about naming of variables colliding with other things in the same namespace (such as count vs counter in my example) 您还需要多加注意在同一命名空间中与其他事物冲突的变量的命名(例如,在我的示例中为count vs counter)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM