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

python setattr not checking if variable exists

test file: my_name = '' my_home_address = '' my_home_phone = '' my_office_address = '' my_office_phone = '' This another file, lets call it test2 import test line1 = ['address', 'home', 'tom', 'downtown', '12345'] line1 = ['address', 'office',…
Hyder Tom
  • 373
  • 4
  • 14
-1
votes
1 answer

Prevent global variables of called submodules from being altered without changing the submodul itself?

I am trying to write some kind of wrapper for testing python module. This wrapper simulates an environment and then starts other python module via execfile. There are many python modules to be tested (>200). Inside those modules there are some…
and0r
  • 305
  • 1
  • 4
  • 13
-2
votes
1 answer

How to not mess up self.__dict__ generation when using self.__setattr__()

I'm trying to implement special behavior, so that values in self and self.aux_gate are always the same. I tried doing this by implementing __setattr__(), but in doing this, I've ran in many problems regarding __dict__. From what I could gather, it…
Therk
  • 15
  • 4
-2
votes
1 answer

Maya setAttr ed

Sorry guys maybe the question is stupid or even I ask in a wrong way. But I'm totally new with Maya. So hope can get some help here. Thank you so much! The question is: I have a .ma file, and it has something like setAttr ".ed[6474:6639]" 8053 8052…
Bensvage
  • 9
  • 1
  • 1
-2
votes
2 answers

Python create list of objects from list of 2 elements lists

I have a list of lists of 2-element lists, e.g.: [[[a, b], [c, d], [e, f]], [[g, h], [i, j], [k, l]]] and I want to create a list of objects with attributes from the two-element lists, like this: [(obj.a = b, obj.c=d, obj.e=f), (obj.g=h, obj.i=j,…
Viker
  • 21
  • 1
  • 4
-4
votes
2 answers

python object style access for dictionaries ; cant figure it out

class ObjectDict(dict): """ allows object style access for dictionaries """ def __getattr__(self, name): if name in self: return self[name] else: raise AttributeError('No such attribute: %s' % name) …
Arjun Biju
  • 73
  • 9
-6
votes
1 answer

How do I use getattr and setattr properly in Python?

I created a class in Python called Student. I need to create five instances of the class, and then be able to return an attribute from an instance of the user's choice, as well as set an attribute to a new value. Here is my code: class Student: …
Jasonca1
  • 4,848
  • 6
  • 25
  • 42
1 2 3
13
14