在类python之外发出调用方法 [英] Issue calling method outside class python

查看:55
本文介绍了在类python之外发出调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理涉及创建和使用具有多种方法的类的 Python 评估.我不会发布整个内容,因为如果我改为展示我正在尝试做的事情的示例,那么指出我的问题会更简单.

I'm working on a python assingment involving creating and using a class with several methods. I won't post the whole thing as it'll be simpler to point out my issue if I instead show an example of what I'm trying to do.

class Fruit(self,color,name):
    def __init__(self, color, name):
        self.color = color
        self.name = name

    def return_list(self, index):
        print fruit_list[index]

fruit_list = []

fruit_1 = Fruit("red", "apple")
fruit_list.append(fruit_1.color) 
fruit_list.append(fruit_1.name)

所以上面的代码有效.我的问题是在课堂外使用时让 return_list 起作用,如下所示:

So the above code works. My issue is getting return_list to work when used outside of the class, as so:

fruit_choice = int(raw_input("What fruit would you like?"))
Fruit.return_list(fruit_choice)

基本上,我正在尝试创建一个方法,该方法在调用时输出列表中索引处的项目,该索引由用户输入指定(即fruit_choice = 0,打印的项目是列表的第一个元素.)

Basically I'm trying to create a method that when called, outputs the item at an index within a list, the index being specified by user input (i.e fruit_choice = 0, printed item is first element of list.)

我对类有一个基本的了解,但是得到一个有点模糊的方法来像 return_list 一样工作有点不直观.我得到的错误信息:

I have a basic understanding of classes, but getting a somewhat vague method to work like return_list is a little unintuitive. The error message I get:

Fruit.return_list(fruit_choice)
TypeError: unbound method return_list() must be called with Fruit instance as first argument (got int instance instead)

我知道如果我让代码工作,如果输入为 0 而不是red,"apple",输出将是red",但这是另一个问题.

I know that if I got the code to work the output would be "red" if input was 0 instead of "red,"apple", but that's an issue for another time.

如果有人能指出我在创建/调用 return_list 方法时做错了什么,谢谢.

If someone could point out to me what I'm doing wrong in creating/calling the return_list method, please and thanks.

推荐答案

您似乎对类和类实例有误解.

You seem to have a misunderstanding of Classes and class instances.

您定义的 return_list 函数是 Fruit 类的一部分,即 Fruit 类的每个实例都包含此方法.但是这个函数的使用,即从水果列表中返回一个特定的水果,是独立于各个Fruit实例的,这意味着这个方法不应该出现在Fruit中> 首先上课.

The return_list function you have defined is part of the Fruit class, i.e. each instance of the Fruit class contains this method. But the use of this function, which is to return a specific fruit from fruit list, is independent of the individual Fruit instances, which means this method should not have been in the Fruit class in the first place.

类定义class Fruit(self,color,name)的签名错误.它应该是 class Fruit:.初始参数应定义为 __init__ 方法的参数.

The signature of class definition class Fruit(self,color,name) is wrong. It should be class Fruit:. The initial parameters should be defined as parameters to the __init__ method.

列表fruit_list 应包含Fruit 实例.程序可以写成:

The list fruit_list should contain Fruit instances. The program could be written as:

class Fruit:
    def __init__(self, color, name):
        self.color = color
        self.name = name

    def __str__(self):
        return 'I am a ' + self.color + ' ' + self.name

fruit_list = []

fruit_1 = Fruit("red", "apple")
fruit_list.append(fruit_1)

fruit_choice = int(raw_input("What fruit would you like?"))
print(fruit_list[fruit_choice])

打印 Python 对象会调用 __str__ 魔术方法.

Printing a Python object calls the __str__ magic method.

如果你真的想要一个单独的函数从 fruit_list 返回一个 Fruit,你可以在类之外定义一个函数(但是当你可以直接使用索引访问实例):

If you do really want to have a separate function which returns a Fruit from the fruit_list, you can define a function outside the class (but these seem unnecessary when you can directly access the instance using the index):

def return_list(list, index):
    return list[index]

def return_list(index):
    global fruit_list
    return fruit_list[index]

如果您尝试使用 print(fruit_list) 打印整个列表,则输出为:

If you try to print the entire list using print(fruit_list) the output is:

[<__main__.Fruit instance at 0x110091cf8>]

这是因为当您打印实例列表时,Python 不会在每个实例上调用 __str__.您可以通过执行以下操作打印 fruit_list:

That is because Python does not call __str__ on each instance when you print a list of instances. You can print the fruit_list by doing:

for fruit in fruit_list:
    print(fruit)

这篇关于在类python之外发出调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆