实例作为第一个参数的未绑定方法获取了字符串,但还需要其他内容 [英] unbound method with instance as first argument got string but requires something else

查看:72
本文介绍了实例作为第一个参数的未绑定方法获取了字符串,但还需要其他内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#Maps.py
class Maps(object):

    def __init__(self):
        self.animals = []
        self.currently_occupied = {}

    def add_animal(self, name):
        self.animals.append(name)
        self.currently_occupied = {robot:[0, 0]}



#animal.py
class Animal(object):

    def __init__(self, name):
        import maps
        maps.add_animal(rbt)
        self.name = name


#Tproject.py
from Animal import Animal
Fred = Animal("Fred")

给我一​​个看起来像这样的错误

gives me this an error that looks like this

TypeError:未绑定方法add_animal()必须以Maps实例作为第一个参数(而不是got str实例)来调用

TypeError: unbound method add_animal() must be called with Maps instance as first argument (got str instance instead)

但我不知道这是什么意思,我无法通过Google或Yahoo搜索来解决它:(

but i dont know what it means and i cannot figure it out searching through google or yahoo :(

推荐答案

您需要一个Maps实例,而不是Maps类:

You need an instance of Maps, not the Maps class:

 maps.Maps.add_animal("Fred") # gives error

 mymap = maps.Map()

 mymap.add_animal("Fred") # should work

因此,您应该在Animal类上,每个Animal实例上或作为全局对象(无论哪种情况最适合您的情况)都具有mymap属性.

So you should either have a mymap attribute on the Animal class, per Animal instance or as a global object (whatever works best for your case).

这篇关于实例作为第一个参数的未绑定方法获取了字符串,但还需要其他内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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