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
4
votes
6 answers

Python setattr() can't find attribute that obviously exists

I'm at my wits end. Can't find anything else that helps with this. dta = {'type': "", 'content': ""} print dta >>>{'content': '', 'type': ''} setattr(dta, 'type', "Steve") >>>AttributeError: 'dict' object has no attribute 'type'
Steve
  • 905
  • 1
  • 8
  • 32
4
votes
1 answer

Allow super to control __setattr__ on subclasses

This question is about the read-only problem for objects that are based on a super() and if/how super can/should control __setattr__ on subclasses. Context: Is there a way to write a meta class or a descriptor such that all classes that are…
root-11
  • 1,727
  • 1
  • 19
  • 33
4
votes
2 answers

python setattr for dynamic method creator with decorator

I have a class which has multiple methods defined. import mat class Klass(object): @mat.sell(mat.CanSet): def method1(self): return None @mat.sell(mat.CanSet): def method2(self): return 'value2' Imagine I have 10…
LuckyStarr
  • 1,465
  • 2
  • 12
  • 14
4
votes
1 answer

Cython unable to use setattr

I have a cdef class in cython and I want to initialize its fields with the setattr builtin function. However, when I do that I got an execution error : /path/.../cimul.cpython-34m.so in cimul.Simulation.__init__ (cimul.c:5100)() AttributeError:…
cphyc
  • 440
  • 5
  • 16
4
votes
2 answers

AttributeError when using object.__setattr__

What's wrong with the last three lines? class FooClass(object): pass bar1 = object() bar2 = object() bar3 = object() foo1 = FooClass() foo2 = FooClass() foo3 = FooClass()…
jimifiki
  • 5,377
  • 2
  • 34
  • 60
3
votes
2 answers

setattr, object deletion and cyclic garbage collection

I would like to understand how object deletion works on python. Here is a very simple bunch of code. class A(object): def __init__(self): setattr(self, "test", self._test) def _test(self): print "Hello, World!" def…
ohe
  • 3,461
  • 3
  • 26
  • 50
3
votes
1 answer

Python classes, difference between setting an attribute and using __setattr__()

I'm trying to set attributes to a class of which I don't know the name a-priori. I also want to avoid users to write to that attribute, so I use a property factory with getters and setters which returns a property object. However, when calling the…
Mezzerine
  • 33
  • 3
3
votes
2 answers

Automatically add decorator to all inherited methods

I want in class B to automatically add the decorator _preCheck to all methods that have been inherited from class A. In the example b.double(5) is correctly called with the wrapper. I want to avoid to manually re-declare (override) the inherited…
3
votes
1 answer

Python: __setattr__ as class-method // self.__dict__ assignment on a metaclass

I want to create a simple Class which just stores / holds data as class attributes without creating an instance as an object. Simple as that which can be accessed from everywhere in my code: class DATA: foo = {} bar = {} Now I wanted to go…
tim
  • 9,896
  • 20
  • 81
  • 137
3
votes
1 answer

Creating Django model fields dynamically from method variables during migration

One of my app's models has a few pre-defined fields, let's say: class Data(models.Model) user = models.CharField(max_length=50) date = models.DateTimeField() It also has a few fields that are based on variables passed in to the methods of a…
toastyrye
  • 31
  • 3
3
votes
1 answer

Is setattr calling property setter func?

I try to fit good oop behaviour in my code and was wondering the following: I generally have this kind of a class design: class MyClass(): def __init__(self,arg1): self.arg1 = arg1 @property def arg1(self): return…
MaKaNu
  • 762
  • 8
  • 25
3
votes
2 answers

Is __setattr__ called when an existing attribute is incremented?

Is __setattr__ called when an existing attribute is incremented? I have a class called C, and I'm trying to overload __setattr__. This is a section of code from inside the class C: class C: def bump(self): self.a += 1 self.b +=…
Justin Yum
  • 189
  • 2
  • 12
3
votes
1 answer

Python: unpack init args into member variables cleanly

Consider the example class below: class Foo(object): def __init__(self, a=2, b=0, c=1, d=42): self.a = a self.b = b self.c = c self.d = d Is there a clean way to unpack the kwargs into member variables? Perhaps…
BoltzmannBrain
  • 5,082
  • 11
  • 46
  • 79
3
votes
2 answers

__getattr__ throwing maximum recursion error when __setattr__ implemented

I'm trying to create a circle class using the magic methods __getattr__ and __setattr__, and I seem to have my __getattr__ working, but when I implement __setattr__ (which should only allow the values for x and y to be set if the value is an int,…
BrxttB
  • 103
  • 1
  • 9
3
votes
2 answers

What tests can I perform to ensure I have overridden __setattr__ correctly?

I have been studying Python for a little while now, and I've come to understand that overriding __setattr__ correctly can be troublesome (to say the least!). What are some effective ways to ensure/prove to myself the override has been done…
Rick
  • 43,029
  • 15
  • 76
  • 119
1 2
3
13 14