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
1
vote
0 answers

Make a custom class that inherits TextIOWrapper

I am trying to make a method in a class which returns an object that behaves like a buffered file stream (python's built-in class, TextIOWrapper), Plus, I am trying to add some more features(method) on it. It would look similar to: class ReadFile: …
dropyourcoffee
  • 318
  • 1
  • 5
  • 13
1
vote
1 answer

python __setattr__ causes attribute error on a defined variable

Every time the 'setattr' magic method is called it causes an attribute error for some reason. I'm getting an attribute error saying the 'rect' variable doesn't exist but it's clearly defined in the class. import pygame as pg class Block: …
Baryon
  • 112
  • 10
1
vote
0 answers

Python descriptors set and get behaviour

Say, we put two different descriptors in a class, and then check how they work being requested from an instance and a class itself. class DescrGet: def __get__(self, instance, typ): print("DescrGet") class DescrSet: def…
A.King
  • 172
  • 2
  • 11
1
vote
1 answer

Dynamic Instance variables within Class Scope

I've got a class that is a descendant of SQLAlchemy's declarative base. I need to write a bridge object that will translate between the declarative base and another system I am running, but I want the bridge object to not have to know exactly what…
A. Wilson
  • 8,534
  • 1
  • 26
  • 39
1
vote
3 answers

How to differentiate between a property being set with ___setattr__ from inside the class and outside the class?

Is there any way in __setattr__() to differentiate between an attribute set from inside the class or a child/inheriting class, and an attribute set from outside the current or a child class? I want to change how setting attributes works from the…
laundmo
  • 318
  • 5
  • 16
1
vote
2 answers

How to use setattr if value is not single?

I want to use setattr to create a plot: import numpy as np import matplotlib.pyplot as plt x = np.random.rand(10) y = np.random.rand(10) # I want this: # plt.scatter(x, y) setattr(plt, "scatter", [HOW TO PASS X AND…
ivanacorovic
  • 2,669
  • 4
  • 30
  • 46
1
vote
1 answer

How to use setattr to create bound methods?

So I have a class with a method, which takes string. Somethinkg like this: class A(): def func(self, name): # do some stuff with it I have finite number of possible values, [val1, val2, val2] for example, All strings. I want to use…
Michail Highkhan
  • 517
  • 6
  • 18
1
vote
1 answer

How to add a module's attributes to a class with custom behavior

I have a module in some path. I want to create a class with the same attributes as the module (so that it can be used the same way as the module) but perform some custom actions before accessing the attributes - such as reloading the module. def…
felisimo
  • 198
  • 14
1
vote
2 answers

Cannot update mutable instance attributes when __setattr__ and __getattribute__ are overridden in python

I have inherited a bunch of legacy code and have run into a bit of a breaking issue. I believe the idea attempted here was to use a containerized cache such as redis to hold certain attributes to be shared across many processes. Apart from whether…
Kyle
  • 11
  • 1
1
vote
2 answers

Inheritance without inheriting, is there create dinamically attributes that point to other class variables?

I'm struggling to solve with this problem. I'd like to have the name variable to be like a pointer to the value of self.model.name. If the value of _class.model.name is changed the _class.name should change accordingly. Can't find a way to…
1
vote
1 answer

Setting Property via a String

I'm trying to set a Python class property outside of the class via the setattr(self, item, value) function. class MyClass: def getMyProperty(self): return self.__my_property def setMyProperty(self, value): if value is None: …
E-rich
  • 9,243
  • 11
  • 48
  • 79
1
vote
1 answer

setattr() not setting

I have a class (bot), which has an attribute "health"; since there are a lot of parameters to this class, and I wished for the user to input a lot of them, I chose to loop through a dict of {param:explanation}, and for each param, input a value to…
zhang
  • 11
  • 1
1
vote
1 answer

Python: Inject attribute into object created by C library

I'm trying to inject an attribute into lxml.etree._Element, but as that module is completely implemented in C, setattr fails: Traceback (most recent call last): [...] setattr(node.getroottree().getroot(), "attributeName", value) AttributeError:…
AndiDog
  • 68,631
  • 21
  • 159
  • 205
1
vote
2 answers

`setattr()` on Python2 `_sre.SRE_Pattern`

I'm basically attempting py2/3 compatibility by trying to add a a fullmatch method to Python2 re compiled patterns. I've seen https://stackoverflow.com/a/2982/2694385 on how to add an attribute to an existing instance. I'm creating the method as…
user80551
  • 1,134
  • 2
  • 15
  • 26
1
vote
4 answers

Python 2.7: Is it good practice to set all args/kwargs attribute with setattr?

In this answer it is shown how to automatically set as attribute the kwargs/args passed to a class __init__ For example, one could do: class Employee(object): def __init__(self, *initial_data, **kwargs): # EDIT …
FLab
  • 7,136
  • 5
  • 36
  • 69