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

How to propergate change detection to outer object in python?

I have an object of objects and want to know when a sub-object has changed. I understand how to use __setattr__ for primitive types but do not understand how to use it with objects. I guess python uses points and that is way __setattr__ is not…
HennyKo
  • 712
  • 1
  • 8
  • 19
0
votes
1 answer

Why using __dict__ in __setattr__ causing infinite loop in __getattr__

I wrote a working program for this specified simplified Mesh class, but I can not make it work for real class with dozens of methods/properties. I can not modify real Mesh class, and I can not make Object class extended Mesh. This works fine: …
0
votes
2 answers

infinite recursion when overriding __setattr__

I want to create the class 'Human' with attributes "name" and "gender". I want to restrict assignment of the "gender" attribute to only "male" or "female". For this purpose, we have overridden __setattr__(self, name, value). class Human(object): …
Girish Gupta
  • 1,241
  • 13
  • 27
0
votes
1 answer

setattr doesn't update the correct attribute of a class when using string

I'm trying to update an attribute of a class with setattr and a string. So for instance with this code: class ClassA(): def __init__(self): self.A = 0 class ClassB(): def __init__(self): self.CA = ClassA() CB =…
ymmx
  • 4,769
  • 5
  • 32
  • 64
0
votes
1 answer

extending a class that uses __getattr__ (pygame.Rect)

I am trying to create my own version of a pygame.Rect rectangle, but with the added feature that when a square is out of certain worldbounds, it appears on the other side. This means I had to rewrite a lot of functions of pygame.Rect in my…
0
votes
2 answers

calling setattr before 'self' is returned

I suspect this is kind of a klugefest on my part, but I'm working with the Luigi and Sciluigi modules which set a number of critical parameters PRIOR to 'self' being returned by an init. ANd if I try to manhandle these parameters AFTER self is…
RightmireM
  • 2,381
  • 2
  • 24
  • 42
0
votes
1 answer

setattr() not assigning value to class member

I'm trying to set up a simple test example of setattr() in Python, but it fails to assign a new value to the member. class Foo(object): __bar = 0 def modify_bar(self): print(self.__bar) setattr(self, "__bar", 1) …
A. Luo
  • 3
  • 1
0
votes
0 answers

setattr not passing self to methods added to class

I have a class: class A(object): def __init__(self): self._first=1 def a(self): pass I want to dynamically add a method to it, but I want the name to be based on input (in my case, based on coded object attributes, but…
Alex Barry
  • 415
  • 1
  • 9
  • 21
0
votes
1 answer

Defining an inner class using the Python C-API

In Python, it’s straightforward to define an inner class: class MyClass(object): class MyInnerClass(object): pass … which the inner class can be accessed as one would expect, e.g. by doing MyClass.MyInnerClass. I am trying to set up…
fish2000
  • 4,289
  • 2
  • 37
  • 76
0
votes
0 answers

Using __setattr__ + __slots__ in a python3 class

I am trying to be all fancy with sub element attribute access in a custom class hierarchy. My fanciness works in that I can successfully use descriptors to do this. I want to be even more fancy and make the class RefHolder (shown below in the…
Gregory Kuhn
  • 1,627
  • 2
  • 22
  • 34
0
votes
0 answers

Dynamically Adding Methods to Python Class

I have a dictionary of classes: classes = { 'cls1' : Class1, 'cls2' : Class2 } Here Class1, Class2 are Python classes I've defined, and each of them have methods foo() and bar(). Lets say I have another class called OtherClass. I want to iterate…
Hiranya Jayathilaka
  • 7,180
  • 1
  • 23
  • 34
0
votes
2 answers

Python purpose of % attribute prefix

I've seen Python objects with attributes like %values. I can't access such attributes, as they raise a SyntaxError. They also seem to require setattr to be created without a SyntaxError: class A(object): pass a = A() setattr(a, "%values",…
user2561747
  • 1,333
  • 2
  • 16
  • 39
0
votes
1 answer

python __setattr__ attribute issue

I have a class designed to work with postgresql db: dbclass = DBclass(1) class DBclass(object): select_query = 'SELECT * FROM "{table}" WHERE {table}_id=%s' def __init__(self, id=None): self.__id = id …
ovod
  • 1,118
  • 4
  • 18
  • 33
0
votes
1 answer

python spyder editor autocompletion and inspection of modified classes with setattr

after spending hours in googling, I learned a lot, but did not find any close topic to the following: I am currently programming on dynamically created classes via a factory method. The reason is, that the method names should be populated according…
PyPhreak
  • 3
  • 1
  • 2
0
votes
1 answer

What is the difference between a float and a double in maya?

In Maya, when using the setAttr() command, we can use float and double as a variable type. What is the difference? The example in the documentation is the same one. -type float3 Array of three floats Value Syntax float float float Value Meaning …
user3296643
  • 1
  • 1
  • 1