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

create a class attribute without going through __setattr__

What I have below is a class I made to easily store a bunch of data as attributes. They wind up getting stored in a dictionary. I override __getattr__ and __setattr__ to store and retrieve the values back in different types of units. When I started…
eric.frederich
  • 1,598
  • 4
  • 17
  • 30
2
votes
1 answer

Use built-in setattr simultaneously with index slicing

A class I am writing requires the use of variable-name attributes storing numpy arrays. I would like to assign values to slices of these arrays. I have been using setattr so that I can leave the attribute name to vary. My attempts to assign values…
aph
  • 1,765
  • 2
  • 19
  • 34
2
votes
1 answer

Is there a way to get an error using setattr which would I get without using setattr with django models?

If I use setattr(p,'wrong_attr','value') instead of p=MyModel.objects.filter(WRONG_ATTRIBUTE=value) I don't get the error related to the non existent attribute. Is there a way to get that error using setattr? Thank you.
foebu
  • 1,365
  • 2
  • 18
  • 35
2
votes
1 answer

Django: Calling save() on FileField using field name as string

I am new to Django and already have read and tried everything that came into my mind. I have a Model-based Form. The model includes 3 FileField fields: img_big = models.FileField(upload_to='images/%Y/%m/b',max_length=100) img_med =…
horbor
  • 609
  • 7
  • 13
2
votes
2 answers

exec to add a function into a class

So I've looked at similar questions, and I've found some solutions to this, but I can't quite figure out how to do this. What I'm trying to do is add a method to a class from a string. I can do this with the setattr() method, but that won't let me…
Hovestar
  • 1,544
  • 4
  • 18
  • 26
2
votes
1 answer

Switchable dot access to Python dicts?

I haven't seen a toggleable version of dictionary dot-access yet. My first-pass attempt here doesn't work: class DottableDict (dict): def allowDotting (self, state=True): if state: self.__setattr__ = dict.__setitem__ …
Gary Fixler
  • 5,632
  • 2
  • 23
  • 39
2
votes
2 answers

How can I dynamically add a function to an instance in python upon instantiation

If I have a python class which allows for an options parameter upon instantiation, how can I dynamically set a function to it, based upon the value of that options parameter. For example, if I have the code def hello1(): print(self.name,"says…
user1876508
  • 12,864
  • 21
  • 68
  • 105
2
votes
1 answer

Python: differences among __setitem__ ; setattr ; set

simple explanations by examples would do where the 3 of them cannot be interchanged. def __setitem__(self,**k): #self.val=k for key in k: self.val.setdefault(key,[]).extend(v for v in k[key]) Can the above step be done…
user2290820
  • 2,709
  • 5
  • 34
  • 62
1
vote
1 answer

Python class as datastore

I'm trying to write a Python class that acts like some sort of datastore. So instead of using a dictionary for example, I want to access my data as class.foo and still be able to do all the cool stuff like iterate over it as for x in dataclass. The…
Timo
  • 164
  • 2
  • 12
1
vote
1 answer

__setattr__ class decorator python

I'm using a class decorator but I didn't understand how set attribute with setattr, this is my code: def cldecor(*par): def onDecorator(aClass): class wrapper: def __init__(self, *args): self.wrapped =…
fege
  • 547
  • 3
  • 7
  • 19
1
vote
1 answer

What is difference between self[name] and self.__dict__[name] in Python dictionary class?

I am not quite familiar with inheritance in Python, what is difference in following code between self.name and self.__dict__[name]? And if test = AttrDict(), why I can use dot to set attribute for test such like test.x = 100? class AttrDict(dict): …
HeII0W0rId
  • 31
  • 3
1
vote
1 answer

Dynamically creating read-only attributes

I'm currently trying to create a class that inherits from set, but allows calling for subsets with attributes. Since I want to create this class such that I can use it in any context, I want to have it be able to create attributes from any given…
1
vote
1 answer

Python: How to return an instance of an object along with attributes assigned with setattr

I am still very new to python, but I need to interface with some software that is written as a bunch of python modules (.py files in case I incorrectly identified them as "modules.") This program has some very useful and complicated functions that I…
Alex Eftimiades
  • 2,527
  • 3
  • 24
  • 33
1
vote
1 answer

Using __getattr__() and __setattr__() to provide multiple ways to access child variables

Esteemed colleagues! Please consider... #!/usr/bin/env python3.6 class Child(object): def __init__(self, name:str, value = 0): self.name = name; self.value = value; def __repr__(self): return ' child "{}" has value…
Lance E.T. Compte
  • 932
  • 1
  • 11
  • 33
1
vote
1 answer

Override class methods (e.g. __setattr__) after __init__()

So I've recently come up with a botched abomination of a class while I was trying to implement some sort of DotDict that would: Enable key-value dot-access. Accept a defaults Namespace/Dataclass as an extra argument containing default…
ohno
  • 31
  • 2