English 中文(简体)
姓名:姓名自称[复制]
原标题:NameError: name self is not defined [duplicate]

为什么这种结构

class A:
    def __init__(self, a):
        self.a = a

    def p(self, b=self.a):
        print b

缩略语 姓名:姓名自称没有定义?

最佳回答

缺省性论点值按职能界定的时间加以评价,但<代码>本身<>/代码>只是在职能要求时间时才提供论据。 因此,论点清单中的论点不能相互提及。

这是一种常见的情况,即存在一个论点,即<条码>None,并在法典中添加检验标准:

def p(self, b=None):
    if b is None:
        b = self.a
    print b

<><>Update 2022: 甲型六氯环己烷开发商现在为,考虑过迟到的违约论点,供今后露宿。

问题回答

如果你也希望选择设立b至零:

def p(self, **kwargs):
    b = kwargs.get( b , self.a)
    print b

缩略语 如果你不能在方法签字中自我界定,也可能发生。 这一错误通常作为<代码>TypeError出现,因为预期和特定论点之间会有不匹配之处。 但是,如果您接受一系列不同的论据,<代码>自行<>/code>将改为arg[0],而<代码>本身将不予界定。

一个最低的例子。

class Obj:
    def foo(*args):
        print(self.bar)

>NameError: 姓名自称没有定义

教养:

class Obj:
    def baz(self, *args):
        print(self.bar)

http://neopythonic.blogspot.com/2008/10/why-explicit- Self-has-to-stay.html





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签