Python实例没有调用方法

2024-06-01 18:56:58 发布

您现在位置:Python中文网/ 问答频道 /正文

在放弃一个丑陋的bash脚本之后,我一直在学习如何更好地使用Python。

我试图使用两个类来定义一些对象数组,其中存储一些唯一的字符串和整数(1-10)。对象将包括以下内容:

object[i].user
         .n     # n = i
         .name
         .coords
         .hero

(param1,param2,param3)对于每个object.n和object.user都是不同的,所以我试图使用一个赋值方法,在写了90个唯一的字符串之后,它看起来不像垃圾。我发现嵌套一个示例不起作用,所以这里有一个折衷方案:

class CityBean:
    def __init__(self,name,coords,hero):
        self.name = name
        self.coords = coords
        self.hero = hero

class Castles:
    def __init__(self,user,n):
        self.user = user
        self.n = n
        if self.user == 'user1':
            temp = {
                1:  CityBean( "name1"  , "coord1" , "hero1"),
                ... blah blah blah
                10: CityBean( "name10" , "coord10" , "hero10" )}[self.n]()
        if self.user == 'user2':
            temp = {
                1:  CityBean( "name11" , "coord11" , "hero11" ),
                ... blah blah blah
                10: CityBean( "name20" , "coord20" , "hero20" ) }[self.n]()
        if self.user == 'user3':
            temp = {
                1:  CityBean( "name21" , "coord21" , "hero21" ),
                ... blah blah blah
                10: CityBean( "name30" , "coord30" , "hero30" ) }[self.n]()
        self.name = temp.name
        self.coords = temp.coords
        self.hero = temp.coords
        __del__(temp)

我这样称呼它:

cities = list( Castles("user2",i) for i in range(1,11) )

它给了我这个错误:

AttributeError: CityBean instance has no __call__ method

它责怪这句话:

                10: CityBean( "name20" , "coord20" , "hero20" ) }[self.n]() # pseudo
                10: CityBean( "" , "" , "" ) }[self.n]() # what is actually looks like

我的粗俗课怎么了?我在做一些弱智的事情,不是吗?


Tags: 对象字符串nameselfifobjectinitdef
3条回答

从你提供的内容很难猜出你想要什么,因为你提供了你的新手代码,而不是说你想做什么,所以一个人很难猜到。

我想这样可以:

data = {
        (1, 'user1'): ("name1", "coord1", "hero1"),
        (2, 'user1'): ("name2", "coord2", "hero2"),
        #...
        (1, 'user2'): ("name11", "coord11", "hero11"),
        (2, 'user2'): ("name12", "coord12", "hero12"),
        # ...
    }


class CityBean:
    def __init__(self,name,coords,hero):
        self.name = name
        self.coords = coords
        self.hero = hero

class Castles:
    def __init__(self,user,n):
        self.user = user
        self.n = n
        name, coords, hero = data.get((n, user))
        self.citybean = CityBean(name, coords, hero)

你在做什么

{
1:  CityBean( "name1"  , "coord1" , "hero1"),
... blah blah blah
10: CityBean( "name10" , "coord10" , "hero10" )}[self.n]()

基本上是从dict基于key的get value中得到的,dict上的值是CityBean实例,所以简而言之

CityBean( "name1"  , "coord1" , "hero1")()

这是有效的,但将调用实例的特殊方法__call__,因此根据需要删除()或添加__call__方法

你为什么写这个?

temp = {
            1:  CityBean( "name21" , "coord21" , "hero21" ),
            ... blah blah blah
            10: CityBean( "name30" , "coord30" , "hero30" ) }[self.n]()

你认为temp= {...}[something]()能做什么?

  1. 它创造了一本字典。{...}

  2. 它从字典里挑了一项。{...}[something]。这将是一个CityBean对象。

  3. 它计算一个项作为函数{...}[something]()CityBean(...)()

为什么要像调用函数一样调用CityBean对象?

此外,为什么创建整个词典只是为了挑选其中的一个条目?if语句有什么问题?

相关问题 更多 >