Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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中创建对象?_Python_List_Oop_Object - Fatal编程技术网

在遍历列表中的元素时,如何在python中创建对象?

在遍历列表中的元素时,如何在python中创建对象?,python,list,oop,object,Python,List,Oop,Object,我试图将列表中的元素传递给类,以便在Python中创建对象。稍后,当我使用相同的列表尝试调用该对象时,我得到错误:“str”对象没有属性“name” 我已经使用Python有一段时间了,但对OOP来说还是新手。想知道这是否与对象的范围有关> class SwimmingWithTheFishes: def __init__(self, typeofshark): self.name = typeofshark def __str__(self):

我试图将列表中的元素传递给类,以便在Python中创建对象。稍后,当我使用相同的列表尝试调用该对象时,我得到错误:“str”对象没有属性“name”

我已经使用Python有一段时间了,但对OOP来说还是新手。想知道这是否与对象的范围有关>

class SwimmingWithTheFishes:
    def __init__(self, typeofshark):
        self.name = typeofshark

    def __str__(self):
        return f"This is the method shark: {self.name}"

    def reporting(self, shark):
        name = shark.name
        print(f"This is a method shark: {name}")

    def print_return(self):
        return f'{self.name}'


def main():
    # sharklist = [{"name": "mako"}, {"name": "hammerhead"}, {"name": "greatwhite"}, {"name": "reef"}]
    sharklist = ["mako", "hammerhead", "greatwhite", "reef"]

    for typeofshark in sharklist:
        typeofshark = SwimmingWithTheFishes(typeofshark)
        print(f"Heavens above, that's no fish: {typeofshark.name}")
        typeofshark.reporting(typeofshark)

    for shark in sharklist:
        print(SwimmingWithTheFishes.print_return(shark))


if __name__ == "__main__":
    main() 

print\u return
是一种
swimingwiththefishes
的方法,因此您应该用
shark
实例化
swimingwiththefishes
,这样
self
将成为
swimingwiththefishes
的对象,以便
self.name
print\u return
中工作

更改:

print(SwimmingWithTheFishes.print_return(shark))
致:


当您在列表上迭代并分配给当前变量时,您不会更改列表中的值,只会更改该局部变量

例如


要修改列表,您应该创建一个新列表,因为如果您修改迭代的列表,可能会遇到问题。这个新列表可以称为类似于
sharks
——其中元素包含类实例

最后,你对方法也有误解。。。您不需要每次调用实例上的方法时都传入对对象的引用。method函数的
self
参数自动获取从中调用方法的实例的值

这使得最终代码:

class SwimmingWithTheFishes:
    def __init__(self, typeofshark):
        self.name = typeofshark

    def __str__(self):
        return f"I am a {self.name} shark."

    def reporting(self):
        print(f"This is a {self.name} shark method.") 


def main():
    # shark_types = [{"name": "mako"}, {"name": "hammerhead"}, {"name": "greatwhite"}, {"name": "reef"}]
    shark_types = ["mako", "hammerhead", "greatwhite", "reef"]
    sharks = []

    for type_ in shark_types:
        shark = SwimmingWithTheFishes(type_)
        sharks.append(shark)
        print(f"Heavens above, that's no fish: {shark.name}")
        shark.reporting()

    for shark in sharks:
        print(shark)


if __name__ == "__main__":
    main() 

其中:

Heavens above, that's no fish: mako
This is a mako shark method.
Heavens above, that's no fish: hammerhead
This is a hammerhead shark method.
Heavens above, that's no fish: greatwhite
This is a greatwhite shark method.
Heavens above, that's no fish: reef
This is a reef shark method.
I am a mako shark.
I am a hammerhead shark.
I am a greatwhite shark.
I am a reef shark.

sharklist
是一个字符串列表,因此
shark
是一个字符串。您正在将该字符串传递给
print\u return
,这显然无法工作。非常感谢,这很有意义。谢谢。
class SwimmingWithTheFishes:
    def __init__(self, typeofshark):
        self.name = typeofshark

    def __str__(self):
        return f"I am a {self.name} shark."

    def reporting(self):
        print(f"This is a {self.name} shark method.") 


def main():
    # shark_types = [{"name": "mako"}, {"name": "hammerhead"}, {"name": "greatwhite"}, {"name": "reef"}]
    shark_types = ["mako", "hammerhead", "greatwhite", "reef"]
    sharks = []

    for type_ in shark_types:
        shark = SwimmingWithTheFishes(type_)
        sharks.append(shark)
        print(f"Heavens above, that's no fish: {shark.name}")
        shark.reporting()

    for shark in sharks:
        print(shark)


if __name__ == "__main__":
    main() 
Heavens above, that's no fish: mako
This is a mako shark method.
Heavens above, that's no fish: hammerhead
This is a hammerhead shark method.
Heavens above, that's no fish: greatwhite
This is a greatwhite shark method.
Heavens above, that's no fish: reef
This is a reef shark method.
I am a mako shark.
I am a hammerhead shark.
I am a greatwhite shark.
I am a reef shark.