Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python将生成的Classe对象存储到数组中_Python_Arrays_Class_Object - Fatal编程技术网

Python将生成的Classe对象存储到数组中

Python将生成的Classe对象存储到数组中,python,arrays,class,object,Python,Arrays,Class,Object,我目前正在研究一种方法,生成一组类对象,并将它们存储在数组中,然后执行一个类函数,而不必逐一检查。我提出这个问题的背景与我正在做的并不完全相同,但它太复杂了,无法仅仅解释这个问题 假设我将信息存储在如下数组中: x = [['dog', 1], ['cat',2]] class Animal(object): def __init__(self, name, position): self.name = name self.position = posi

我目前正在研究一种方法,生成一组类对象,并将它们存储在数组中,然后执行一个类函数,而不必逐一检查。我提出这个问题的背景与我正在做的并不完全相同,但它太复杂了,无法仅仅解释这个问题

假设我将信息存储在如下数组中:

x = [['dog', 1], ['cat',2]]
class Animal(object):
    def __init__(self, name, position):
        self.name = name
        self.position = position

    def display(self):
        print(self.name, self.position)
该类将如下所示:

x = [['dog', 1], ['cat',2]]
class Animal(object):
    def __init__(self, name, position):
        self.name = name
        self.position = position

    def display(self):
        print(self.name, self.position)
所以我的目标是从数组中获取信息,并将它们作为对象存储到一个空数组中。这样做的原因是能够从所有对象调用函数,而无需对每个对象重复函数,无需迭代

我下面的问题是,我将它们添加到列表中,但对象没有附加变量名。是否有办法使变量名
值[0]
成为
x
中包含的名称

empty = []

for value in x:
    empty.append(Animal(value[0], value[1]))
那么,一旦正确存储以执行它们,下面的代码会工作吗

for item in empty:
item.display()

我知道你不应该用它来打印值,但我把它放在这个场景中是为了让它简单地处理类。

使用dict来获取字段和值

dog = Animal('dog', 1)
dict_to_store = dog.__dict__
animal = Animal()
animal.__dict_.update(dict_to_store)
在对象上获取字段和值的dict

dog = Animal('dog', 1)
dict_to_store = dog.__dict__
animal = Animal()
animal.__dict_.update(dict_to_store)
对。它将工作(给定适当的缩进)


顺便说一句,获得答案最简单的方法就是运行代码:-)

您的代码工作正常,但可能您正在寻找答案

文件observer.py

class Animal:
    def __init__(self, name, position):
        self.name = name
        self.position = position
    def update(self, message):
        print('{} at {} got message "{}"'.format(self.name, self.position, message))

class Publisher:
    def __init__(self):
        self.subscribers = set()
    def register(self, who):
        self.subscribers.add(who)
    def unregister(self, who):
        self.subscribers.discard(who)
    def dispatch(self, message):
        for subscriber in self.subscribers:
            subscriber.update(message)
from observer import Publisher, Animal

pub = Publisher()

dog = Animal('dog', 1)
cat = Animal('cat', 2)

pub.register(dog)
pub.register(cat)

pub.dispatch("something")

pub.unregister(dog)

pub.dispatch("another thing")
文件驱动程序_observer.py

class Animal:
    def __init__(self, name, position):
        self.name = name
        self.position = position
    def update(self, message):
        print('{} at {} got message "{}"'.format(self.name, self.position, message))

class Publisher:
    def __init__(self):
        self.subscribers = set()
    def register(self, who):
        self.subscribers.add(who)
    def unregister(self, who):
        self.subscribers.discard(who)
    def dispatch(self, message):
        for subscriber in self.subscribers:
            subscriber.update(message)
from observer import Publisher, Animal

pub = Publisher()

dog = Animal('dog', 1)
cat = Animal('cat', 2)

pub.register(dog)
pub.register(cat)

pub.dispatch("something")

pub.unregister(dog)

pub.dispatch("another thing")
执行driver_observer.py时:

dog at 1 got message "something"
cat at 2 got message "something"
cat at 2 got message "another thing"

你试过这个吗?“我正在将它们添加到列表中,但对象没有附加变量名。”。。。是的,列表就是这样工作的。列表中的元素没有变量名。你为什么需要一个?您只需对列表编制索引即可获得所需内容,这样创建的类对象就可以直接存储,而无需首先指定变量名?是的。这也不是用户定义类所独有的。您可以存储内置类型实例,而无需首先为其指定变量名;例如,
my_list.append(23)
而不必做
thing=23;my_list.append(thing)
@Vexoids反过来,变量名被分配给实例。就像一个标签挂在瓶子上。一个瓶子上可以有任意多个标签。你可以拿一个标签挂在另一个瓶子上。