Questions tagged [setattr]

setattr is a Python built-in function used to set a named attribute on an object.

The function's schematics are as follows:

setattr(object:object, name:str, value:object) -> object

where object is the object, name is the named attribute, and value is the value that name will be set to. Below is an example:

>>> class Test:
...     def __init__(self):
...         self.attr = 1
...
>>> myTest = Test()
>>> myTest.attr
1
>>> setattr(myTest, 'attr', 2)
>>> myTest.attr
2
>>>
202 questions
0
votes
2 answers

Python read-only wrapper class that denies access to certain methods and all attributes

I have the following base class. class BaseWithMethod: def __init__(self, prop=None): self.prop = prop def evil_method(self): print(f"%@#&ç? {self.prop}") I want to create a wrapper class ReadonlyWrapperSelectedMethods that…
0
votes
1 answer

Python creating a readonly wrapper class without modifying wrapped class

I have a base class that looks like follows: class Base: def __init__(self, prop): self.prop = prop I want to create a wrapper class ReadonlyWrapper that is read-only, with the following functionality: # Create instance using __init__…
0
votes
2 answers

How to set class property dynamicly in python?

I want to set class property dynamicly. I do it like this: class Foo: def __init__(self): self.a = 1 for v in [1, 2, 3]: setattr(Foo, f"test_{v}", property(lambda self: self.a + v)) foo =…
roger
  • 9,063
  • 20
  • 72
  • 119
0
votes
1 answer

Using setattr() and getattr() in Python descriptors

While trying to create descriptors a few different ways I noticed some strange behavior that I'm trying to understand. Below are the three different ways I have gone about creating descriptors: >>> class NumericValueOne(): ... def __init__(self,…
readytotaste
  • 199
  • 2
  • 4
  • 17
0
votes
1 answer

How to bind a type's `__init__` or `__new__` to some other class instance?

I was trying to set a type attribute to a class using the Python built-in setattr. I've declared __new__ and __init__ methods in the type to see what their parameters would be and surprisingly they're not being bound to receive the class instance.…
Rodrigo Oliveira
  • 1,452
  • 4
  • 19
  • 36
0
votes
1 answer

FastAPI - Set the url or path for the Request object

I have a function that looks like this: @app.middleware("http") async def process_api_event(request: Request, call_next): url = request.url path = request.url.path # request.__setattr__('url', 'sample_url') # request.url.__…
lovprogramming
  • 593
  • 11
  • 24
0
votes
1 answer

applying reflection in python to indirectly call objects from another class

I'm trying to make a class like Proxy that I indirectly access all methods of my objects, for example like: class Radio(): def __init__(self): self._channel = "channel" def get_channel(self): …
0
votes
2 answers

python - dynamically overriding a method at instance level and 'updating' all references

I would like to override the method of an instance of class A with a method from class B but in a way so that all references to the old method of the instance (made before overriding the method) then 'link' to the new one. In code: import…
mep
  • 341
  • 2
  • 11
0
votes
1 answer

How to override a function when Parent already explicitly `setattr` the same function?

A 'minimal' example I created: class C: def wave(self): print("C waves") class A: def __init__(self): c = C() setattr(self, 'wave', getattr(c, 'wave')) class B(A): def wave(self): print("B waves") >>>…
0
votes
1 answer

Edit __init__ method properties into Metaclasse with exec and setattr

I try to modify the __init__ function of a class in the purpose to set as property, the inherited class property, and be able to init the inherited class from the sub class. It should working like this : class B(): def __init__(self, b:str): …
lelchim
  • 45
  • 6
0
votes
0 answers

IDE doesn't see my class object attributes

When I create attributes of class object with setattr() function: class Dict_to_class(): def __init__(self, initDict): for key, value in initDict.items(): setattr(self, key, value) self.class_Names = initDict I can't see what…
S. A.
  • 101
  • 2
  • 11
0
votes
1 answer

Set Maya Image Plane Attr "Image Number" to expression "=frame" through python

I'm trying to set the attribute "Image Number" to the expression "=frame" through python. Currently I've tried cmds.setAttr(myImagePlane+".ufe", 1) cmds.setAttr(myImagePlane+".fe", '=frame') Which doesn't work as .fe only takes integers. From maya…
mhetman
  • 3
  • 2
0
votes
1 answer

setattr does not respect attributes naming conventions

I had a weird encounter the other day, where the following code: class Obj: pass obj = Obj() setattr(obj, '@#^&%$&', None) actually worked. Why is this behavior allowed, if the following raises syntaxerror ? : obj.@#^&%$&
Vasilis Lemonidis
  • 636
  • 1
  • 7
  • 25
0
votes
1 answer

Why does setattr not affect the return value?

I am using setattr() to monkey patch a closure. Using getattr() shows that the closure was patched correctly. However, the return value is unaffected. >>> def foo(): ... def bar(): ... return('bar') ... return(bar()) ... >>> def…
0
votes
2 answers

Getting real caller method name when using setattr()

I have a simple class in which I want to generate methods based on inherited class fields: class Parent: def __init__(self, *args, **kwargs): self.fields = getattr(self, 'TOGGLEABLE') self.generate_methods() def…
wencakisa
  • 5,850
  • 2
  • 15
  • 36