简体   繁体   English

Python - 将列表作为参数传递给类

[英]Python - Passing a list as argument to class

So, I have the following code:所以,我有以下代码:

locations = [0,1]

class Draw:
    def __init__(self, locations):
        self.locations = locations

    def platforms(self):
        print(self.locations)

Draw.platforms(locations)

And the terminal gives me:终端给了我:

AttributeError: 'list' object has no attribute 'locations'

Is there a specific way to pass a list as an argument?有没有一种特定的方法可以将列表作为参数传递? I'm new and I don't understand why it works with a int argument but not with a list.我是新手,我不明白为什么它适用于 int 参数但不适用于列表。

I want to know why this is happening and what can I do about it.我想知道为什么会发生这种情况以及我能做些什么。

Thanks in advance.提前致谢。

You're calling the method "platforms" which is not expecting you to pass it a parameter, and are passing it a parameter.您正在调用方法“平台”,它不希望您将参数传递给它,而是将其传递给它一个参数。 The "locations" list is actually passed to the constructor method of your class, which gets called when you instantiate it. “位置”列表实际上是传递给你的类的构造方法,当你实例化它时会调用它。 Your "platforms" method is called without parameters and prints it.您的“平台”方法在没有参数的情况下被调用并打印出来。

Here's how you should (probably) do it:以下是您应该(可能)这样做的方式:

locations = [0,1]

class Draw:
    def __init__(self, locations):
        self.locations = locations

    def platforms(self):
        print(self.locations)

draw = Draw(locations)    
draw.platforms()

What you're doing at the moment is calling platforms([0, 1]) , which then attempts to resolve您目前正在做的是调用platforms([0, 1]) ,然后尝试解决
[0, 1].locations . [0, 1].locations That's what your error is telling you: list s don't have locations .这就是您的错误告诉您的内容: list没有locations

You need to create a Draw object, with a list argument.您需要使用列表参数创建一个Draw对象。 You can then call the platforms method of that object:然后,您可以调用该对象的platforms方法:

draw = Draw(locations)
draw.platforms()

This could be done with whats called a static method .这可以通过所谓的静态方法来完成。

locations = [0,1]

class Draw:
    def __init__(self, locations):
        self.locations = locations

    @staticmethod
    def platforms(variable):
        print(variable)

Draw.platforms(locations)
 [0, 1]
locations = [0, 1] class Draw: def __init__(self, locations): self.locations = locations def platforms(self): print(self.locations) Draw(locations).platforms()

I don't understand why it works with a int argument but not with a list.我不明白为什么它适用于 int 参数但不适用于列表。

No, it doesn't:不,它不会:

Traceback (most recent call last):
  File "method.py", line 10, in <module>
    Draw.platforms(locations)
  File "method.py", line 8, in platforms
    print(self.locations)
AttributeError: 'int' object has no attribute 'locations'

The reason why it doesn't work in either case is because you never constructed any instance of your class---you just called on a ("public") method belonging to the class.它在任何一种情况下都不起作用的原因是因为您从未构造过类的任何实例——您只是调用了属于该类的(“公共”)方法。 So when platforms() tries to use self.locations , it doesn't exist.因此,当platforms()尝试使用self.locations时,它不存在。

You could either instantiate the object first:您可以先实例化对象:

locations = [0, 1]

drawer = Draw(locations)
drawer.platforms()

Or, if you don't care about keeping the Draw instance around:或者,如果您不关心保留Draw实例:

locations = [0, 1]

Draw(locations).platforms()

Or you change the method so that it accepts an argument instead:或者你改变方法,让它接受一个参数:

class Draw:
    def platforms(value):
        print(value)

But now it's basically a static method, and you're only using the class as a namespace.但现在它基本上是一个静态方法,并且您只是将类用作命名空间。

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

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