setattr

Clean way to disable `__setattr__` until after initialization

≡放荡痞女 提交于 2021-02-04 13:08:06
问题 I've written the following wrapper class. I want to define __setattr__ such that it redirects all attributes to the wrapped class. However, this prevents me from initializing the wrapper class. Any elegant way to fix this? class Wrapper: def __init__(self, value): # How to use the default '__setattr__' inside '__init__'? self.value = value def __setattr__(self, name, value): setattr(self.value, name, value) 回答1: You are catching all assignments, which prevents the constructor from assigning

Clean way to disable `__setattr__` until after initialization

北慕城南 提交于 2021-02-04 13:07:35
问题 I've written the following wrapper class. I want to define __setattr__ such that it redirects all attributes to the wrapped class. However, this prevents me from initializing the wrapper class. Any elegant way to fix this? class Wrapper: def __init__(self, value): # How to use the default '__setattr__' inside '__init__'? self.value = value def __setattr__(self, name, value): setattr(self.value, name, value) 回答1: You are catching all assignments, which prevents the constructor from assigning

How to restrict setting an attribute outside of constructor?

主宰稳场 提交于 2021-01-27 04:01:46
问题 I want to forbid further assignments on some attributes of a class after it was initialized. For instance; no one can explicitly assign any value to 'ssn' (social security number) property after the Person instance 'p' has been initialized. _ setattr _ is also being called while assigning the value inside _ init _ method, thus it is not what I want. I'd like to restrict only further assignments. How can I achieve that? class Person(object): def __init__(self, name, ssn): self.name = name self

Can the usage of `setattr` (and `getattr`) be considered as bad practice?

删除回忆录丶 提交于 2020-12-25 03:50:52
问题 setattr and getattr kind of got into my style of programing (mainly scientific stuff, my knowledge about python is self told). Considering that exec and eval inherit a potential danger since in some cases they might lead to security issues, I was wondering if for setattr the same argument is considered to be valid. (About getattr I found this question which contains some info - although the argumentation is not very convincing.) From what I know, setattr can be used without worrying to much,

Can the usage of `setattr` (and `getattr`) be considered as bad practice?

余生长醉 提交于 2020-12-25 03:49:50
问题 setattr and getattr kind of got into my style of programing (mainly scientific stuff, my knowledge about python is self told). Considering that exec and eval inherit a potential danger since in some cases they might lead to security issues, I was wondering if for setattr the same argument is considered to be valid. (About getattr I found this question which contains some info - although the argumentation is not very convincing.) From what I know, setattr can be used without worrying to much,

Can the usage of `setattr` (and `getattr`) be considered as bad practice?

半世苍凉 提交于 2020-12-25 03:47:37
问题 setattr and getattr kind of got into my style of programing (mainly scientific stuff, my knowledge about python is self told). Considering that exec and eval inherit a potential danger since in some cases they might lead to security issues, I was wondering if for setattr the same argument is considered to be valid. (About getattr I found this question which contains some info - although the argumentation is not very convincing.) From what I know, setattr can be used without worrying to much,

Why does the “name” parameter to __setattr__ include the class, but __getattr__ doesn't?

巧了我就是萌 提交于 2020-01-04 09:03:30
问题 The following code: class MyClass(): def test(self): self.__x = 0 def __setattr__(self, name, value): print name def __getattr__(self, name): print name raise AttributeError(name) x = MyClass() x.test() x.__y Outputs: _MyClass__x __y Traceback (most recent call last): ... AttributeError: __y The documentation is utterly unhelpful stating the "name" is the "name of the attribute", yet for some reason it's different depending on whether you are setting it or getting it. What I want to know is:

setattr and getattr with methods

百般思念 提交于 2020-01-01 10:27:47
问题 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