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
11
votes
1 answer

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

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…
Timm Clarsson
  • 111
  • 1
  • 4
9
votes
3 answers

Accessing list items with getattr/setattr in Python

Trying to access/assign items in a list with getattr and setattr funcions in Python. Unfortunately there seems to be no way of passing the place in the list index along with the list name. Here's some of my tries with some example code: class Lists…
insomniaccanuck
  • 91
  • 1
  • 1
  • 2
9
votes
2 answers

setattr and getattr with methods

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): …
zapatilla
  • 1,711
  • 3
  • 22
  • 38
8
votes
1 answer

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

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': …
bavaza
  • 10,319
  • 10
  • 64
  • 103
8
votes
4 answers

Python recursive setattr()-like function for working with nested dictionaries

There are a lot of good getattr()-like functions for parsing nested dictionary structures, such as: Finding a key recursively in a dictionary Suppose I have a python dictionary , many nests https://gist.github.com/mittenchops/5664038 I would like…
Mittenchops
  • 18,633
  • 33
  • 128
  • 246
7
votes
2 answers

Python setattr() to function takes initial function name

I do understand how setattr() works in python, but my question is when i try to dynamically set an attribute and give it an unbound function as a value, so the attribute is a callable, the attribute ends up taking the name of the unbound function…
nosahama
  • 150
  • 10
7
votes
3 answers

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

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…
Ray Salemi
  • 5,247
  • 4
  • 30
  • 63
7
votes
2 answers

Setting special methods using setattr()

Is it possible to dynamically assign special methods, such as __getitem__, to a class instance using setattr()? For example, if I have this: class Example (object): pass And then try this: >>> example = Example() >>> setattr(example,…
larsks
  • 277,717
  • 41
  • 399
  • 399
6
votes
3 answers

Python - Iterating over Arguments passed to a Function

Suppose I have the following example: class foo: ... def bar(self, w, x, y, z, ...): self.w = w self.x = x self.y = y self.z = z ... I wish to reduce the n-number of attribute assignment lines in bar() to one…
S. Gamgee
  • 501
  • 5
  • 16
6
votes
9 answers

Python __init__ setattr on arguments?

It seems that often __init__ methods are similar to this: def __init__(self, ivar1, ivar2, ivar3): self.ivar1 = ivar1 self.ivar2 = ivar2 self.ivar3 = ivar3 Is there someway to turn the arguments into a list (without resorting to *args…
mk12
  • 25,873
  • 32
  • 98
  • 137
5
votes
3 answers

How can I reach a private variable within the object

I would like to modify an object private variable class Example(): __myTest1 = 1 __myTest2 = 1 def __init__(self): pass def modifyTest(self, name = 'Test1', value): setattr(self, '__my'+name, value); I tried the code…
Mokus
  • 10,174
  • 18
  • 80
  • 122
5
votes
1 answer

Python: adding descriptor with setattr doesn't call __set_name__

This question probably has an answer somewhere, but I couldn't find it. I'm using setattr to add a long list of descriptors to a class, and I have discovered that __set_name__ of the descriptor is not called when I use setattr. It is not a big issue…
Vinzent
  • 1,070
  • 1
  • 9
  • 14
5
votes
2 answers

Python, how to use __setattr__ on dictionary object that is part of the class that overloads the method?

As illustrated in the code below, why can't I use __setattr__ to set values on a dict that is part of the class that overloads the method? I expected that b.hello would not exist. class MyClass(): datastore = {} def __init__(self): …
sowa
  • 1,249
  • 16
  • 29
4
votes
2 answers

need a simple way to add meta information/class to a python list/tuple variable?

All, I want simple meta information to be enclosed on an list object, see below code. >>> a = [] >>> a.foo = 100 Traceback (most recent call last): File "", line 1, in AttributeError: 'list' object has no attribute…
user478514
  • 3,859
  • 10
  • 33
  • 42
4
votes
1 answer

Implementing a dict-like object with __getattr__ and __setattr__ functionality

I'm trying to implement a dict-like object which can be accessed/modified with __getattr__ and __setattr__ for ease of use for my users. The class also implements some other simple functionality. Using this answer as a template, my implementation is…
nivk
  • 685
  • 1
  • 6
  • 21
1
2
3
13 14