setattr

TypeError: __class__ assignment only supported for heap types or ModuleType subclasses

送分小仙女□ 提交于 2019-12-07 12:24:38
问题 I'm trying to copy functions from an arbitrary 'base' class into my new object. However, I'm getting the following error with this sample code. class my_base: def print_hey(): print("HEY") def get_one(): print(1) class my_ext: def __init__(self, base): methods = [method for method in dir(base) if callable(getattr(base, method))] for method in methods: setattr(self, method, getattr(base, method)) me = my_ext(my_base) me.get_one() The above gets this error on the call to setattr . TypeError: _

python setattr for dynamic method creator with decorator

谁说胖子不能爱 提交于 2019-12-06 13:21:40
I have a class which has multiple methods defined. import mat class Klass(object): @mat.sell(mat.CanSet): def method1(self): return None @mat.sell(mat.CanSet): def method2(self): return 'value2' Imagine I have 10 methods that I need to populate for this 'Klass'. I want to generate these methods without explicitely writing them all. So I want to do a factory that does setattr for each method. Problem is that I do following and the last method has the last value. Each do not get its related value but value10. Also below solution does not implement the decorator, I have no idea how to do assign

TypeError: __class__ assignment only supported for heap types or ModuleType subclasses

偶尔善良 提交于 2019-12-06 00:22:49
I'm trying to copy functions from an arbitrary 'base' class into my new object. However, I'm getting the following error with this sample code. class my_base: def print_hey(): print("HEY") def get_one(): print(1) class my_ext: def __init__(self, base): methods = [method for method in dir(base) if callable(getattr(base, method))] for method in methods: setattr(self, method, getattr(base, method)) me = my_ext(my_base) me.get_one() The above gets this error on the call to setattr . TypeError: __class__ assignment only supported for heap types or ModuleType subclasses The statement works if I type

Can __setattr__() can be defined in a class with __slots__?

我是研究僧i 提交于 2019-12-04 11:27:17
问题 Say I have a class which defines __slots__ : class Foo(object): __slots__ = ['x'] def __init__(self, x=1): self.x = x # will the following work? def __setattr__(self, key, value): if key == 'x': object.__setattr__(self, name, -value) # Haha - let's set to minus x Can I define __setattr__() for it? Since Foo has no __dict__ , what will it update? 回答1: All your code does, apart from negate the value, is call the parent class __setattr__ , which is exactly what would happen without your _

setattr and getattr with methods

雨燕双飞 提交于 2019-12-04 07:29:31
I have a boiler platey class that delegates some actions to a reference class. It looks like this: class MyClass(): def __init__(self, someClass): self.refClass = someClass def action1(self): self.refClass.action1() def action2(self): self.refClass.action2() def action3(self): self.refClass.action3() This is the refClass: class RefClass(): def __init__(self): self.myClass = MyClass(self) def action1(self): #Stuff to execute action1 def action2(self): #Stuff to execute action2 def action3(self): #Stuff to execute action3 I'd like to use Python Metaprogramming to make this more elegant and

Python - how can I dynamically remove a method from a class — i.e. opposite of setattr

久未见 提交于 2019-12-03 12:20:36
I don't know if I have a good design here, but I have a class that is derived from unittest.TestCase and the way I have it set up, my code will dynamically inject a bunch of test_* methods into the class before invoking unittest to run through it. I use setattr for this. This has been working well, but now I have a situation in which I want to remove the methods I previously injected and inject a new set of methods. How can I remove all the methods in a class whose names match the pattern test_* ? It's called delattr and is documented here . >>> class Foo: def func(self): pass ... >>> dir(Foo)

Can __setattr__() can be defined in a class with __slots__?

纵然是瞬间 提交于 2019-12-03 08:12:48
Say I have a class which defines __slots__ : class Foo(object): __slots__ = ['x'] def __init__(self, x=1): self.x = x # will the following work? def __setattr__(self, key, value): if key == 'x': object.__setattr__(self, name, -value) # Haha - let's set to minus x Can I define __setattr__() for it? Since Foo has no __dict__ , what will it update? All your code does, apart from negate the value, is call the parent class __setattr__ , which is exactly what would happen without your __setattr__ method. So the short answer is: Sure you can define a __setattr__ . What you cannot do is redefine _

Setting a class attribute with a given name in python while defining the class

為{幸葍}努か 提交于 2019-12-03 07:16:58
问题 I am trying to do something like this: property = 'name' value = Thing() class A: setattr(A, property, value) other_thing = 'normal attribute' def __init__(self, etc) #etc.......... But I can't seem to find the reference to the class to get the setattr to work the same as just assigning a variable in the class definition. How can I do this? 回答1: You'll need to use a metaclass for this: property = 'foo' value = 'bar' class MC(type): def __init__(cls, name, bases, dict): setattr(cls, property,

Setting a class attribute with a given name in python while defining the class

邮差的信 提交于 2019-12-02 23:17:51
I am trying to do something like this: property = 'name' value = Thing() class A: setattr(A, property, value) other_thing = 'normal attribute' def __init__(self, etc) #etc.......... But I can't seem to find the reference to the class to get the setattr to work the same as just assigning a variable in the class definition. How can I do this? You'll need to use a metaclass for this: property = 'foo' value = 'bar' class MC(type): def __init__(cls, name, bases, dict): setattr(cls, property, value) super(MC, cls).__init__(name, bases, dict) class C(object): __metaclass__ = MC print C.foo You can do

setattr() not assigning value to class member

笑着哭i 提交于 2019-12-02 19:04:52
问题 I'm trying to set up a simple test example of setattr() in Python, but it fails to assign a new value to the member. class Foo(object): __bar = 0 def modify_bar(self): print(self.__bar) setattr(self, "__bar", 1) print(self.__bar) Here I tried variable assignment with setattr(self, "bar", 1) , but was unsuccessful: >>> foo = Foo() >>> foo.modify_bar() 0 0 Can someone explain what is happening under the hood. I'm new to python, so please forgive my elementary question. 回答1: A leading double