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

How do I call setattr() on the current module?

What do I pass as the first parameter "object" to the function setattr(object, name, value), to set variables on the current module? For example: setattr(object, "SOME_CONSTANT", 42); giving the same effect as: SOME_CONSTANT = 42 within the module…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
87
votes
6 answers

Using setattr() in python

I am looking for someone to explain the basics of how to use, and not use setattr(). My problem arose trying to use one class method/function to return data that is then put in another method/function. Perhaps a simpler approach would be much better…
bounce
  • 981
  • 1
  • 6
  • 5
80
votes
6 answers

How can I dynamically create class methods for a class in python

If I define a little python program as class a(): def _func(self): return "asdf" # Not sure what to resplace __init__ with so that a.func will return asdf def __init__(self, *args, **kwargs): setattr(self, 'func',…
user1876508
  • 12,864
  • 21
  • 68
  • 105
80
votes
5 answers

How to use __setattr__ correctly, avoiding infinite recursion

I want to define a class containing read and write methods, which can be called as follows: instance.read instance.write instance.device.read instance.device.write To not use interlaced classes, my idea was to overwrite the __getattr__ and…
Alex
  • 41,580
  • 88
  • 260
  • 469
66
votes
12 answers

getattr and setattr on nested subobjects / chained properties?

I have an object (Person) that has multiple subobjects (Pet, Residence) as properties. I want to be able to dynamically set the properties of these subobjects like so: class Person(object): def __init__(self): self.pet = Pet() …
TPB
  • 737
  • 1
  • 6
  • 8
49
votes
3 answers

How do I properly override __setattr__ and __getattribute__ on new-style classes in Python?

I want to override my Python class's __getattribute__ and __setattr__ methods. My use case is the usual one: I have a few special names that I want to handle, and I want the default behavior for anything else. For __getattribute__, it seems that I…
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159
44
votes
2 answers

Difference between setattr and object manipulation in python/django

I have the following model: class Ticket(models.Model): title = models.CharField() merged_to = models.ForeignKey("self", related_name='merger_ticket', null=True, blank=True) looser_ticket = models.BooleanField(default=False) There are…
whatf
  • 6,378
  • 14
  • 49
  • 78
18
votes
2 answers

Why does setattr fail on a bound method

In the following, setattr succeeds in the first invocation, but fails in the second, with: AttributeError: 'method' object has no attribute 'i' Why is this, and is there a way of setting an attribute on a method such that it will only exist on one…
tjm
  • 7,500
  • 2
  • 32
  • 53
18
votes
1 answer

What's the difference between setattr() and object.__setattr__()?

I know that you can't call object.__setattr__ on objects not inherited from object, but is there anything else that is different between the two? I'm working in Python 2.6, if this matters.
paper.cut
  • 235
  • 1
  • 3
  • 7
18
votes
4 answers

Setting a class attribute with a given name in python while defining the class

I am trying to do something like this: property = 'name' value = Thing() class A: setattr(A, property, value) other_thing = 'normal attribute' def __init__(self, etc) #etc.......... But I can't seem to find the reference to the class to…
prismofeverything
  • 8,799
  • 8
  • 38
  • 53
18
votes
4 answers

Python - how can I dynamically remove a method from a class -- i.e. opposite of setattr

I don't know if I have a good design here, but I have a class that is derived from unittest.TestCase and the way I have it set up, my code will dynamically inject a bunch of test_* methods into the class before invoking unittest to run through it. …
tadasajon
  • 14,276
  • 29
  • 92
  • 144
18
votes
4 answers

Clean way to disable `__setattr__` until after initialization

I've written the following wrapper class. I want to define __setattr__ such that it redirects all attributes to the wrapped class. However, this prevents me from initializing the wrapper class. Any elegant way to fix this? class Wrapper: def…
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
15
votes
4 answers

Python: inconsistence in the way you define the function __setattr__?

Consider this code: class Foo1(dict): def __getattr__(self, key): return self[key] def __setattr__(self, key, value): self[key] = value class Foo2(dict): __getattr__ = dict.__getitem__ __setattr__ = dict.__setitem__ o1 =…
Albert
  • 65,406
  • 61
  • 242
  • 386
15
votes
1 answer

How do i set extended user attributes on android files?

Is there a way for my android app to retrieve and set extended user attributes of files? Is there a way to use java.nio.file.Files on android? Is there any way to use setfattr and getfattr from my dalvik app? I know that android use the ext4 file…
pismakron
  • 151
  • 1
  • 5
15
votes
5 answers

How to restrict setting an attribute outside of constructor?

I want to forbid further assignments on some attributes of a class after it was initialized. For instance; no one can explicitly assign any value to 'ssn' (social security number) property after the Person instance 'p' has been initialized.…
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
1
2 3
13 14