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
3
votes
2 answers

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

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 =…
Draemon
  • 33,955
  • 16
  • 77
  • 104
3
votes
2 answers

maximum recursion depth while using __setattr__ in python new style object?

I have the following code which comprises of a person class and a Manager class that delegates to a person class. I am using new style object(derived from object) and running python 2.7. I geth maximum recursion depth which I am unable to…
brain storm
  • 30,124
  • 69
  • 225
  • 393
3
votes
3 answers

I'd like to create a mock object in python

I want a dummy object I can instantiate in python and programmatically create attributes for via setattr(). I tried it on the built in object but probably for a good reason that didn't work. What base object can I use in python for such purposes…
user1561108
  • 2,666
  • 9
  • 44
  • 69
3
votes
3 answers

How to create a property with its name in a string?

Using Python I want to create a property in a class, but having the name of it in a string. Normally you do: blah = property(get_blah, set_blah, del_blah, "bleh blih") where get_, set_ and del_blah have been defined accordingly. I've tried to do…
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
3
votes
1 answer

Is there a nicer way to do a getter property for dynamically named attribute?

I have a number of similar fields in one of my classes modelling json data. All fields are initialized to None to help static tools know they exist then helper functions help initialize them based on a piece of json data that they are modelling (The…
Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141
2
votes
3 answers

__setattr__ only for names not found in the object's attributes`?

I want to use __setattr__ only when the attribute was not found in the object's attributes, like __getattr__. Do I really have to use try-except? def __setattr__(self, name, value): try: setattr(super(Clazz, self), name, value) …
Niklas R
  • 16,299
  • 28
  • 108
  • 203
2
votes
7 answers

python koans: class proxy

I'm solving the python koans. I haven't got any real problem until the 34th. this is the problem: Project: Create a Proxy Class In this assignment, create a proxy class (one is started for you below). You should be able to initialize the proxy…
kurojishi
  • 321
  • 1
  • 4
  • 12
2
votes
1 answer

Typing a dynamically assigned attribute

I am writing a "plugin" for an existing python package and wondering how I would properly type-hint something like this. Scenario A python object from a framework I am working with has a plugins attribute that is designed to be extended by…
Ed May
  • 75
  • 3
2
votes
0 answers

How to initialize object attributes from an array of strings in Python?

I am supposed to write a class which will allow the user of the script to define what attributes will it's objects have when initializing them by giving it an array of strings like this. data = Database(tables=['distance', 'speed']) Then it should…
2
votes
4 answers

How can i add atrributes to the class method from outside class?

This is gonna be my first question on this website. If I do mistake about English, sorry. Okey, my question is how can I add or set attribute to the class method from outside class? If i am not wrong,we use settatr() to do this. Can you guys help me…
Berke Balcı
  • 81
  • 10
2
votes
1 answer

How to create properties at runtime with Python?

So I'm trying to figure out if what I want to do is even possible. I am writing some test code for an application, and I have objects that contain properties representing some of the elements we have in the interface for our product. What I want to…
Silas Ray
  • 25,682
  • 5
  • 48
  • 63
2
votes
0 answers

Detect attribute change in python class for computational expensive dependent attributes

I am having the following Python problem: I have a class with different attributes that depend on each other. If an attribute is changed on runtime, some attributes should change as well. I am aware of getters and the @property decorator. However,…
2
votes
2 answers

Can dynamically created class methods know their 'created' name at runtime?

I have a class which I want to use to extract data from a text file (already parsed) and I want do so using dynamically created class methods, because otherwise there would be a lot of repetitive code. Each created class method shall be asociated…
2
votes
3 answers

Using __setattr__ and __getattr__ for delegation with __slots__ without triggering infinite recursion

class A: __slots__ = ("a",) def __init__(self) -> None: self.a = 1 class B1: __slots__ = ("b",) def __init__(self, b) -> None: self.b = b def __getattr__(self, k): return getattr(self.b, k) def…
KOLANICH
  • 2,904
  • 2
  • 20
  • 20
2
votes
1 answer

How to use __setitem__ properly?

I want to make a data object: class GameData: def __init__(self, data={}): self.data = data def __getitem__(self, item): return self.data[item] def __setitem__(self, key, value): self.data[key] = value def __getattr__(self,…
Just_Me
  • 111
  • 8