使用__getattribute__或__getattr__在Python中调用方法

AJ *_*ord 10 python getattr getattribute

我正在尝试创建一个子类,作为自定义类的列表.但是,我希望列表继承父类的方法和属性,并返回每个项的数量总和.我试图使用该__getattribute__方法执行此操作,但我无法弄清楚如何将参数传递给可调用属性.下面的高度简化的代码应该更清楚地解释.

class Product:
    def __init__(self,price,quantity):
        self.price=price
        self.quantity=quantity
    def get_total_price(self,tax_rate):
        return self.price*self.quantity*(1+tax_rate)

class Package(Product,list):
    def __init__(self,*args):
        list.__init__(self,args)
    def __getattribute__(self,*args):
        name = args[0]
    # the only argument passed is the name...
        if name in dir(self[0]):
            tot = 0
            for product in self:
                tot += getattr(product,name)#(need some way to pass the argument)
            return sum
        else:
            list.__getattribute__(self,*args)

p1 = Product(2,4)
p2 = Product(1,6)

print p1.get_total_price(0.1) # returns 8.8
print p2.get_total_price(0.1) # returns 6.6

pkg = Package(p1,p2)
print pkg.get_total_price(0.1) #desired output is 15.4.
Run Code Online (Sandbox Code Playgroud)

实际上,我有许多必须可调用的父类方法.我意识到我可以手动覆盖每个类似列表的子类,但我想避免这种情况,因为将来可能会向父类添加更多方法,我想要一个动态系统.任何建议或意见表示赞赏.谢谢!

Cat*_*lus 10

这段代码非常糟糕,根本不是Pythonic.没有办法让你传递额外的参数__getattribute__,所以你不应该尝试像这样做任何隐含的魔法.它会更好地写成这样:

class Product(object):
    def __init__(self, price, quantity):
        self.price    = price
        self.quantity = quantity

    def get_total_price(self, tax_rate):
        return self.price * self.quantity * (1 + tax_rate)

class Package(object):
    def __init__(self, *products):
        self.products = products

    def get_total_price(self, tax_rate):
        return sum(P.get_total_price(tax_rate) for P in self.products)
Run Code Online (Sandbox Code Playgroud)

如果需要,可以使包装器更通用,比如

class Package(object):
    def __init__(self, *products):
        self.products = products

    def sum_with(self, method, *args):
        return sum(getattr(P, method)(*args) for P in self.products)

    def get_total_price(self, tax_rate):
        return self.sum_with('get_total_price', tax_rate)

    def another_method(self, foo, bar):
        return self.sum_with('another_method', foo, bar)

    # or just use sum_with directly
Run Code Online (Sandbox Code Playgroud)

显式优于隐式.组成通常也比继承更好.


Ned*_*der 8

你在这里有几点困惑:

1)__getattribute__拦截所有属性访问,这不是你想要的.如果真实属性不存在,您只希望代码插入,因此您需要__getattr__.

2)你__getattribute__正在调用列表元素上的方法,但它不应该做真正的工作,它应该只返回一个可调用的东西.请记住,在Python中,x.m(a)实际上是两个步骤:首先,获取x.m,然后使用参数调用该事物a.您的功能应该只是第一步,而不是两个步骤.

3)我很惊讶你需要覆盖的所有方法都应该加总.真的有很多方法,真的应该总结一下,以使这个值得吗?

此代码可以满足您的需求,但您可能需要考虑更明确的方法,正如其他人所建议的那样:

class Product:
    def __init__(self,price,quantity):
        self.price = price
        self.quantity = quantity

    def get_total_price(self,tax_rate):
        return self.price*self.quantity*(1+tax_rate)

class Package(list):
    def __init__(self,*args):
        list.__init__(self,args)

    def __getattr__(self,name):
        if hasattr(self[0], name):
            def fn(*args):
                tot = 0
                for product in self:
                    tot += getattr(product,name)(*args)
                return tot
            return fn
        else:
            raise AttributeError
Run Code Online (Sandbox Code Playgroud)

在这段代码中需要注意的事项:我Package没有从中获取Product,因为它的所有产品都是从委托到列表元素的.不要in dir()用来决定一件东西是否有属性,请使用hasattr.