简体   繁体   English

调用 class 中另一个方法的返回值的方法

[英]call method on returned value of another method in class

So I want to call method on returned value from another method of same class.所以我想从同一个 class 的另一个方法调用返回值的方法。

class A():

    def __init__(self,data):
        self.data = data

    def values(self,key):
        return list(list(x.values())[0] for x in self.data['data'] if key in x.keys())

    def length(self):
        return len(self)

data ={"data":[{"country":"india"},{"state":"punjab"},{"country":"usa"}]}
obj = A(data)
res = obj.values('country') # returns ['india', 'usa']
res1 = obj.values('country').length() #  returns AttributeError: 'list' object has no attribute 'length'
print(res)
print(res1)

i want both res and res1 to work.我希望 res 和 res1 都能工作。

I have also tried using @property decorator我也尝试过使用@property 装饰器

class B():
    def __init__(self,data):
        self.data = data

    def __call__(self, key):
        self.key = key
        return self

    @property
    def values(self):
        self.valu =  list(list(x.values())[0] for x in self.data['data'] if self.key in x.keys())
        return self

    def length(self):
        return len(self.valu)

data ={"data":[{"country":"india"},{"state":"punjab"},{"country":"usa"}]}
obj = B(data)
res = obj('country').values.length()     #  returns 2
res1 = obj('country').values      # returns <__main__.A object at 0x103a9fbe0>
print(res)
print(res1)

This way res works but res1 does not.这种方式 res 工作,但 res1 没有。

Thanks谢谢

.length() is not a python list function. .length()不是 python 列表 function。 Use len(listobject) instead.改用len(listobject)

Eg:例如:

len(obj('country').values)

If you want to print the values.如果要打印值。 In your second example:在您的第二个示例中:

#Use 
res1 = obj('country').data

#instead of
res1 = obj('country').values

Use below code to get list of countries使用以下代码获取国家/地区列表


res1 = [_dict.get('country') for _dict in obj('country').data.get('data') if 'country' in _dict.keys()]

print(res1)
#['india', 'usa']

Final Updated working code:最终更新的工作代码:

class B():
    def __init__(self,data):
        self.data = data

    def __call__(self, key):
        self.key = key
        return self

    @property
    def values(self):
        self.valu =  list(list(x.values())[0] for x in self.data['data'] if self.key in x.keys())
        return self

    def length(self):
        return len(self.valu)

data ={"data":[{"country":"india"},{"state":"punjab"},{"country":"usa"}]}
obj = B(data)
res = obj('country').values.length()     #  returns 2
res1 = [_dict.get('country') for _dict in obj('country').data.get('data') if 'country' in _dict.keys()]      # returns ['usa', 'india']
print(res)
print(res1)

I think the confusion here is on what object is the .length method invoked.我认为这里的困惑在于 object 是调用的.length方法。 In the first case, it is invoked on a list which does not have a .length method.在第一种情况下,它是在没有.length方法的list上调用的。 In the second case, it is invoked on the B object which indeed has a .length method.在第二种情况下,它在确实具有.length方法的B object 上调用。 The most straightforward and preferred solution is what @Subhrajyoti Das suggested.最直接和首选的解决方案是@Subhrajyoti Das 建议的。

If you still want your code (as described in the question) to work (or just want to know if this could actually be done), you could define a custom list object which would look like as follows:如果您仍然希望您的代码(如问题中所述)工作(或只是想知道这是否可以实际完成),您可以定义一个自定义列表 object 如下所示:

class CustomList(list):
    def length(self):
        return len(self)

Now instead of making a list , you would make a CustomList .现在,您将创建一个CustomList ,而不是制作一个list

Edit: Adding the complete example, as requested in the comment.编辑:按照评论中的要求添加完整的示例。

class CustomList(list):
    def length(self):
        return len(self)

class A():

    def __init__(self,data):
        self.data = data

    def values(self,key):
        return CustomList(CustomList(x.values())[0] for x in self.data['data'] if key in x.keys())

    def length(self):
        return len(self)

data ={"data":[{"country":"india"},{"state":"punjab"},{"country":"usa"}]}
obj = A(data)
res = obj.values('country') # returns ['india', 'usa']
res1 = obj.values('country').length() # 2
object has no attribute 'length'
print(res)
print(res1)

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

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