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

How to change kivy NumericProperty object atrribute by pressing button in other screen

In our app, we have a GameScreen that has a NumericProperty object that displays a player's score (GameScreen_player1_score). On a separate ClueScreen (using kivy Screen Manager) we have a ClueAnswerButton1 that should change the player's score on…
guitarical
  • 169
  • 4
  • 14
1
vote
2 answers

__setattr__ method to ensure objects in the class are immutable

I have the following class defined (a big part of it shortened down) but my main question is once an object of this class is defined with its initalized variables I want to make sure that these variables are unchangeable from outside of the class...…
1
vote
2 answers

Adding properties dynamically using functions created dynamically

I would like to implement something that would work like this: memo = Note("memo",5) report = Note("report",20) notebook = Notebook(memo,report) print str(notebook.memo) # 5 expected print str(notebook.report) # 20 expected Inspired…
Vince
  • 3,979
  • 10
  • 41
  • 69
1
vote
2 answers

How can I replace `eval` in my function?

I know very well that it is not good practice using eval in Python. Here is my code: from hashlib import * # added for clarification def get_hash(self): if self.hash_type in ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512']: …
Deneb
  • 119
  • 1
  • 2
  • 10
1
vote
2 answers

Attribute aliasing using __setattr__ in Python

I have a Python class which has various internal variables that I want to expose as x, y, a simple example being: class Vec2: def __init__(self): self.data = [0.0, 0.0] self.mapping = {'x':0, 'y':1} def __getitem__(self,…
Robotbugs
  • 4,307
  • 3
  • 22
  • 30
1
vote
2 answers

__setattr__ versus __slots__ for constraining attribute creation in Python

I've been reading about how make Python classes less dynamic, specifically by not allowing users to dynamically create new attributes. I've read that overloadding __setattr__ is a good way to do this , and __slots__ is not the way to go. One post on…
hunse
  • 3,175
  • 20
  • 25
1
vote
3 answers

Overridden attribute access does not work (as expected)

The main objective of the following module, is to provide a kind of "constant" semantics for some names. class ConstantError(Exception): def __init__(self, msg): self._msg = msg class Constant(object): def __init__(self,…
ht.
  • 25
  • 4
1
vote
2 answers

Python combining setattr and getattr

I would like to update the attributes of a class dynamically, but it seems that the combination of setattr and getattr does not work as I would like to use it. Here is my main class: class Container(object): def __init__(self): …
malekcellier
  • 177
  • 3
  • 10
1
vote
3 answers

how to set an attribute in a nested object in Python?

I'm making my first attempts at Python. I need to loop over a log, parse log entries and then update an object, which includes nested objects for machines listed in the log. This is what I have: import re format_pat= re.compile( …
frequent
  • 27,643
  • 59
  • 181
  • 333
1
vote
1 answer

How can I clean up use of exec, can’t get setattr to do the job?

I want to check an arbitrary (defined in data) set of rules expressed in text and eval() does the job nicely. e.g. to define a rule to check that A and B are both valid: Rule = "A and B" print eval(Rule) So how do I dynamically assign values to an…
paaste
  • 13
  • 2
0
votes
2 answers

GetAttr Function Problems (Python 3)

I have the following in a Python script: setattr(stringRESULTS, "b", b) Which gives me the following error: AttributeError: 'str' object has no attribute 'b' Can any-one telling me what the problem is here?
Eden Crow
  • 14,684
  • 11
  • 26
  • 24
0
votes
1 answer

How to un-override __setattr__ method?

The goal of this question is to understand how "built in" are the built-in functions in python. If I override setattr method, how can I get it back? For example, I have class "Working" like this: class WorkingSetAttr: def a(self): return 'a' …
Juha
  • 2,053
  • 23
  • 44
0
votes
0 answers

How to set attribute with index in the name in python

I'm trying to setattr() using name list. When calling it the names containing index number in square brackets raises AttributeError in Python 2 and TypeError in Python. class NodeClass(): def __init__(self): self.name = "NodeName" …
IKA
  • 151
  • 1
  • 3
  • 11
0
votes
0 answers

Excluding an attribute from __setattr__ and __getattr__ in Python?

I'm trying to implement a Config class that holds and keeps track of an arbitrary amount of Settings. I'd like to get and set the Settings like c = Config(); c.x = 1; print(c.foo) where x and foo are some Settings (or…
nieswand
  • 33
  • 7
0
votes
0 answers

AttributeError and RecursionError happened when using `__setattr__` in python

I used super() as a part of the __setattr__ method to update a dictionary whenever an attribute value is set on an instance of a class; however, a warning reported as my class object has no such attribute '_attributes'. The code just like…