小编Emm*_*wan的帖子

Python setattr()函数采用初始函数名称

我确实了解setattr()python的工作原理,但是我的问题是当我尝试动态设置属性并将其未绑定函数作为值提供时,因此该属性是可调用的,当我使用该属性时,该属性最终会使用未绑定函数的名称调用attr.__name__,而不是属性的名称。

这是一个例子:

我有一Filter堂课:

class Filter:
    def __init__(self, column=['poi_id', 'tp.event'], access=['con', 'don']):
        self.column = column
        self.access = access
        self.accessor_column = dict(zip(self.access, self.column))
        self.set_conditions()

    def condition(self, name):
        # i want to be able to get the name of the dynamically set 
        # function and check `self.accessor_column` for a value, but when
        # i do `setattr(self, 'accessor', self.condition)`, the function 
        # name is always set to `condition` rather than `accessor`
        return name

    def set_conditions(self):
        mapping = list(zip(self.column, …
Run Code Online (Sandbox Code Playgroud)

python class setattribute setattr dynamic-function

5
推荐指数
1
解决办法
89
查看次数

标签 统计

class ×1

dynamic-function ×1

python ×1

setattr ×1

setattribute ×1