繁体   English   中英

为什么在逻辑上它不应该在Python鼻子测试中工作

[英]Why is this working in Python nosetests when logically it should not

class TestUM:

    @classmethod
    def setup_class(will):
        """ Setup Class"""

        will.var = "TEST"

    def setup(this):
        """ Setup """

        print this.var

    def test_number(work):
        """ Method """

        print work.var


    def teardown(orr):
        """ Teardown """

        print orr.var

    @classmethod
    def teardown_class(nott):
        """ Teardown Class """

        print nott.var

运行为

鼻子测试-v -s test.py

我不是Python专家,但是我无法弄清楚为什么上面的代码使用鼻子可以完美地工作。 每个打印都打印“ TEST”。 这里到底发生了什么。

在实例方法中,第一个参数是实例本身。

在类方法中,第一个参数是类本身。

就您而言,不是将其命名为selfcls (约定),而是将其命名为thisworkorrnott 但是无论参数名称如何,他们都得到相同的参数。

您已成功将var属性设置为"TEST" ,因此他们都可以正确看到它。


不使用类的示例函数:

def test1(attribute):
    print attribute

def test2(name):
    print name

def test3(cls):
    print cls

def test4(self):
    print self

调用这些函数:

>>> test1('hello')
hello
>>> test2('hello')
hello
>>> test3('hello')
hello
>>> test4('hello')
hello

参数的名称无关紧要。 重要的是参数要指向的内容,它始终是实例或类

暂无
暂无

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

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