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

How to save Pytorch tensor attributes to disk?

I used setattr to save some information with a Pytorch Tensor, which can be retrieved as expected: import torch before_write = torch.Tensor() setattr(before_write, "features", "my_features") print(before_write.features) >…
MartijnVanAttekum
  • 1,405
  • 12
  • 20
2
votes
0 answers

Python: How to escape special __setattr__() in the __init__() method

I want to use a Python class that recomputes certain attributes whenever other attributes are changed. In an older question (for Python 2) Martijn Pieters♦ suggested using setattr hook , whenever one wants to use a setter without using the getter…
2
votes
2 answers

Why does an object with redefined __getattr__() throws TypeError?

Here is the code class MyTest: def __init__(self): pass def __getattr__(self, attr): pass t = MyTest() print 'my test object: %r' %t So print triggers a TypeError: 'NoneType' object is not callable while i only want to…
anverx
  • 107
  • 1
  • 7
2
votes
0 answers

setattribute for classvariable

I am trying to set a class attribute basically redisconnection object once the connection is established to a variable in another class. I use setattr to set the variable. When i try to access the variable the value is null. import importlib import…
Balachandar
  • 1,538
  • 3
  • 16
  • 25
2
votes
1 answer

the fundamental differences of the way to overwrite getattr and setattr

My hope is to make attributes case-insensitive. But overwriting __getattr__ and __setattr__ are somewhat different, as indicated by the following toy example: class A(object): x = 10 def __getattr__(self, attribute): return…
J. Lin
  • 139
  • 3
  • 11
2
votes
1 answer

define_method in Python

I would like to dynamically define some methods for Python class. I googled for a while and found this. I changed the code a bit to fulfill my requirement. Here's my codes: class Base(object): def add_method(self, field): def func(self,…
mCY
  • 2,731
  • 7
  • 25
  • 43
2
votes
2 answers

How do you invoke a method on a programmatically set attribute?

This is a follow-up question to How do you programmatically set an attribute?. I see how I can set an attribute value using setattr(x, attr, value). How might I invoke a method on attr? For one example, I want to create and set the value of Tkinter…
2
votes
1 answer

Cannot manually assign new parameters via layer name

I'm trying to manually assign new weights to my pytorch model. I can assign new weights like this: import scipy.io as sio import torch caffe_params = sio.loadmat('export_conv1_1.mat') net.conv1_1.weight =…
mcExchange
  • 6,154
  • 12
  • 57
  • 103
2
votes
1 answer

dynamically insert rows to table flask sqlalchemy

I want to insert new rows to an sql table dynamically in SQLAlchemy. I basically want to do this, but instead of doing it with an UPDATE, I want to do it with an INSERT. I am using Flask and WTForms. Right now, I have to add a new user doing…
Ryan Skene
  • 864
  • 1
  • 12
  • 29
2
votes
1 answer

Combining __setattr__ and __getattr__ causes infinite loop

I'm trying to build a system where a base class is used for every other object. Every base object has a _fields dictionary internally where implementations of the base class can store their information. Base class implementation is quite…
Yonathan
  • 1,253
  • 1
  • 17
  • 29
2
votes
0 answers

Can an __init__ function in a class be added to or altered without overwriting it?

In Python 3.x with the given class hierarchy, where SubClassZero and SubClassOne inherit from SuperClass; class SuperClass(object): def __init__(self, *args, **kwargs): self.thing_zero = "Inherited string." class…
James Draper
  • 5,110
  • 5
  • 40
  • 59
2
votes
2 answers

Python setattr vs __setattr__ UnicodeEncodeError

I know that we have to use setattr method when we are outside of an object. However, I have troubles calling setattr with unicode key leading me to use __setattr__ directly. class MyObject(object): def __init__(self): …
2
votes
2 answers

Using Python setattr to set an attribute reference to a method or function

I need to use setattr to read from a configuration file, and set an attribute of a class to a function in another module. Roughly, here's the issue. This works: class MyClass: def __init__(self): self.number_cruncher =…
user5747140
  • 197
  • 1
  • 8
2
votes
2 answers

How to call __setattr__() correctly in Python3 as part of a class?

I want to call a function when an object attribute is set: class MyClass(): myattrib = None def __setattr__(self, prop, val): self.myattrib = val print("setting myattrib") x = MyClass() x.myattrib = "something" The problem…
polemon
  • 4,722
  • 3
  • 37
  • 48
2
votes
3 answers

How do you bind methods to objects in python during runtime?

I want to add a method to an object during runtime as thus. class C(object): def __init__(self, value) self.value = value obj = C('test') def f(self): print self.value setattr(obj, 'f', f) obj.f() Traceback (most recent call last): …
Irvan
  • 439
  • 4
  • 19