English 中文(简体)
如何在最初的班级减员中填写空名单?
原标题:How to make empty list in initial attr of class?
  • 时间:2012-04-10 08:43:58
  •  标签:
  • python
  • oop

这里是一部法典:

>>> class A(object):
...     value = []
...     def method(self, new_value):
...         self.value.append(new_value)
... 
>>> a = A()
>>> a.value
[]
>>> a.method(1)
>>> b = A()
>>> b.value
[1]
>>> b.method(2)
>>> b.value
[1, 2]
>>> a.value
[1, 2]

This happens only with lists. Is the only way to deffine value in __init__?
How to normally define default class values in python?

<><>UPD>/strong>

感谢你的答复

>>> class B(object):
...     value = "str"
...     def method(self):
...         self.value += "1"
... 
>>> a = B()
>>> a.value
 str 
>>> a.method()
>>> a.value
 str1 
>>> b = B()
>>> b.value
 str 

我看不出,为什么要分享名单,但又不是?

最佳回答

您定义的<条码>值/代码”并非您类别的一个实例,更像是静态的领域。 但是,如果你能够从某些情况进入这个领域,那么就 p了。 因此,即使你能够从个案中进入这个领域,它也不是不同的例子。 基本上,每当使用同一方法时,就把你看上去。

你们不得不这样做。

class A(object):

    def __init__(self):
        self.value = []

    def method(self, new_value):
        self.value.append(new_value)

现在,每个例子都有一个不同的清单。

<EDIT>/em>: 请允许我解释一下在你使用<条码>时发生的情况。

class A(object):

    self.value =  str 

    def method(self):
        self.value +=  1 

前一部法典的最后一行是:

        self.value = self.value +  1 

Now, this makes it abit easier to see what s going on. First, python gets the value from self.value. Since there is no instance field defined yet on self, this will give str . Add 1 to that and sets it to the instance field called value. This is like

self.value =  str1 

which is the same as you d set an instance field in the __init__ method (in my first snippet of code).

self.value = []

这是否明确了?

问题回答

Define Value in _init__()。 也没有其他办法界定属性。

不属于案件处理方法的辅助器具是类别属性,由该类别的所有情况共同使用。 因此,正如你在你的榜样中指出的那样,对受等级属性约束的物体的修改影响到所有类别。





相关问题
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 ]="...

热门标签