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

Different functions, same result

So I'm trying to dynamically set properties to a class with different names where each property makes a unique call to my database. However, when I access the property objects, all of them return the same result despite looking up on different…
Exa
  • 185
  • 12
1
vote
3 answers

Is it possible to dynamically name attributes in an App Engine Model?

setattr allows you to dynamically name attributes in Python classes. I'm trying to do something similar with an App Engine Model: class MyModel(db.Model): def __init__(self, *args, **kwargs): super(MyModel, self).__init__(*args,…
Matt Norris
  • 8,596
  • 14
  • 59
  • 90
1
vote
0 answers

arbitrary __getattr__ fails on classes that inherit object but not on object

Can anyone tell why this piece of code fails: object.__setattr__(object(), 'non-existing-attribute', 200) But this not: class K(object): pass object.__setattr__(K(), 'non-existing-attribute', 200)
Oded Badt
  • 323
  • 1
  • 3
  • 11
1
vote
2 answers

Dynamically generated DocType in Elasticsearch DSL

I am generating a DocType class for building mapping and saving documents based upon my ORM. def get_doc_type(self): attributes = {} ... # Build attributes dictionary here DT = type('DocType', (DocType,), attributes) return…
1
vote
2 answers

Using setattr() to update an object instance

I have a model class which has an attribute that refers to django DB objects. I would like to change this attribute using a single view with setattr() which I use to make changes to any attributes for this object. The problem is, I can't seem to…
Catma
  • 39
  • 1
  • 9
1
vote
1 answer

setattr and assignment are not equivalent for private attributes

Using CPython 3.4.3 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6)), i encounter a very weird behavior when using setattr and getattr on private attribute: class Short: def __init__(self): setattr(self, '__myatt', None) self.__myatt = 42 …
aluriak
  • 5,559
  • 2
  • 26
  • 39
1
vote
1 answer

Why favor object.__setattr__(self, name, value) only in new style classes?

According to Python 2.7.12 documentation: If __setattr__() wants to assign to an instance attribute, it should not simply execute self.name = value — this would cause a recursive call to itself. Instead, it should insert the value in the…
nalzok
  • 14,965
  • 21
  • 72
  • 139
1
vote
1 answer

python object member variable missing

I have a constructor which creates a member object of a sub-class: class OpenSprinkler: class CV: my_args = ['rsn', 'rbt', 'en', 'rd', 're'] my_longhand = {'reset_all':'rsn', 'reboot':'rbt', …
devanl
  • 1,232
  • 1
  • 10
  • 20
1
vote
2 answers

__setattr__ function in Python

I am trying to understand the concept of delegation in Python by using the getattr and setattr function . By basic idea is to first set the value of the attribute 'lang' in the Person class through the class Professional and then retrieve the same.…
Subhayan Bhattacharya
  • 5,407
  • 7
  • 42
  • 60
1
vote
2 answers

Using setattr() on a UI element

I am trying to use setattr on a UI element (QLineEdit) to fill in with what was read from a text file. I believe in order to set a QlineEdit it would be self.lineEdit.setText() The text file I am reading consists of a Name and it's…
Zak44
  • 341
  • 1
  • 5
  • 24
1
vote
1 answer

wtforms field created through setattr() loses properties

I am trying to create a form in python / Flask that will add some dynamic slider inputs to a set of standard fields. I am struggling to get it to work properly, though. Most of the web forms in my app are static, created through wtforms as in: …
1
vote
2 answers

settattr for parent class to use in children

I have a library with one parent and a dozen of children: # mylib1.py: # class Foo(object): def __init__(self, a): self.a = a class FooChild(Foo): def __init__(self, a, b): super(FooChild, self).__init__(a) self.b = b # more…
brownian
  • 435
  • 3
  • 13
1
vote
2 answers

Using setattr() to convert strings to variables

I am creating a sigsum() function which takes the sum using an input equation and an input variable. Here's what I have so far: def sigsum(eqn, index, lower=0, upper=None, step=1): if type(step) is not int: raise TypeError('step must be…
Brandon H. Gomes
  • 808
  • 2
  • 9
  • 24
1
vote
1 answer

Perform method when an attribute is added to a class in Python

I would like to instantiate a class, say, Fighter with and without the attribute weapon_size. But when the attribute weapon_size is set (either at instantiation or later) I want to run some method which adds another attribute damage (and perhaps…
p-robot
  • 4,652
  • 2
  • 29
  • 38
1
vote
0 answers

strange behaviour with setattr(x, y, lambda: ...)

I get the following output when I run the code below: 31.688087814 131.569532877 This output is produced by the two lines I have marked out below. The values should be identical(as far as I can see), and should both have the first value. I can't…
Totem
  • 7,189
  • 5
  • 39
  • 66