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
0
votes
0 answers

When to use property() vs __getattr__ and __setattr__ in Python?

When building getter and setter methods in Python, I understand you can define attributes with @property and @x.setter or define the methods __getattr__ and __setattr__. Both ways appear the same to the user as in it replaces directly accessing…
geckels1
  • 347
  • 1
  • 3
  • 13
0
votes
2 answers

Block setting of class attributes (__setattr__)

is there a simple way to prevent setting new class attrs? while trying with the following snippet, shouldn't setattr(Derived, "test1", 1) call the __setattr__ from Base? class Base: def __setattr__(self, key, value): raise…
deponovo
  • 1,114
  • 7
  • 23
0
votes
2 answers

Python-setattr pass function with args

I'm trying to set methods of a class programmatically by calling setattr in a loop, but the reference I pass to the function that is called by this method defaults back to its last value, instead of what was passed at the time of the setattrcall.…
wiseboar
  • 175
  • 2
  • 13
0
votes
0 answers

Google Colab - Infinite recursion error using __getattribute__,__getattr__ and __setattr__?

Suppose I have a class defined as follows: class Employee: def __init__(self,val1,val2): print("In init") self.val1 = val1 self.val2=val2 def __setattr__(self,key,value): print("In setattr") #self.__dict__[key] = value # In…
PHV
  • 39
  • 5
0
votes
0 answers

Multiplication magic method for Gaussian Processes Kernels

I´m creating Kernel classes for Gaussian Processes. Firstly, I created a class "Kernel" that specifies some basic things that every Kernel object must have (still needs improvement). class Kernel: def __init__(self, hypers, sigma_n=None): …
0
votes
0 answers

python setattr function does not set

I have a module and two classes which are called A and B. I would like to record some functions from duvisiopy.model_functions module into class B by using class A then return B. As you can see the output1, they are recorded and they are…
Wtow
  • 98
  • 1
  • 8
0
votes
0 answers

Python __pow__ magic method overwritten in __init__ not called with **

I'm using Python 3.9.12, trying to dynamically overwrite instance methods on instance creation in init for a class (class A) from another class (class B), but getting unexpected results when doing this for the pow magic method. The overwrite seems…
user813869
  • 31
  • 2
0
votes
1 answer

Using setattr() to append to a list attribute

I have an attribute of Class named tag that is a list and I have to append to this list. The attribute name and value are both str variables. I can try setattr(obj, tag, getattr(obj,tag).append(text)) but this creates unnecessary overhead, and also…
kramer
  • 849
  • 2
  • 10
  • 19
0
votes
0 answers

Can I change a list to numpy array if I make a list with setattr(sys.modules[__name__], listname, [])?

I made some variables (list type) with using 'setattr' and 'sys.moduels[_ _ name _ _]'. std_index_lists = {'name1':[8,9,55,193,168,417,285], 'name2':[55, 107, 9, 8, 193, 221, 222, 65], 'name3':[285, 9, 336, 295,…
정지윤
  • 11
  • 2
0
votes
0 answers

What is the 'correct' way to alter one element from a list attribute?

Consider the following piece of code class Point: def __init__(self, x, y): self.x = x self.y = y class Widget: def __init__(self, low=None, mid=None, high=None): self.low = low self.mid = mid …
vshas
  • 49
  • 1
  • 5
0
votes
1 answer

Usage of setattr method in python

I have a question on the usage of the setattr method in python. I have a python class with around 20 attributes, which can be initialized in the below manner: class SomeClass(): def __init__(self, pd_df_row): # pd_df_row is one row from a…
0
votes
1 answer

Not able to setattr using class.__setattr__(key,value)

I have my code like this one: class __Metadata(type): def __init__(cls, name, bases, dict): super().__init__(name, bases, dict) def __setattr__(self, key, value): super().__setattr__(key, value) …
user14073111
  • 647
  • 5
  • 14
0
votes
1 answer

using setattr within a class method to sel something on self

When doing this class Example: def __init__(self, a, b): self.a = a self.b = b def update(self, **kwargs): for key, value in kwargs.items(): getattr(self, key) setattr(self, key, value) ..…
nialloc
  • 805
  • 7
  • 17
0
votes
1 answer

Query Selector giving empty Nodelist in my application

My JS CODE: cell.setAttribute('class','input-cell selected-cell'); //this line is a part of my code. var input_cell=document.querySelectorAll('.input-cell .selected-cell'); console.log(input_cell) I am building an excel clone and while rendering…
0
votes
1 answer

__setattr__ to prohibit changes to instance/self variables?

I have a class Fraction whose init creates the instance variables self.num = num and self.denom=denom: def __init__(self,num=0,denom=1): assert isinstance(num, int), "Numerator and denominator must be an integer" assert…