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

Using setattr to freeze some parameters of a method

in order to automatically generate parameterized tests, I am trying to add methods to a class in by freezing some parameters of an existing method. Here is the piece of Python 3 code class A: def f(self, n): print(n) params =…
Sebastien
  • 246
  • 1
  • 12
0
votes
1 answer

Fix path import when setting attribute inside imported module

To make it shorter, I have a module that does this: MyModule ----mymodule.py ----__init__.py mymodule.py # Get the name of the current module current_module = __import__(__name__) setattr(current_module, 'VarA', 5) So if Im in located inside the…
PepperoniPizza
  • 8,842
  • 9
  • 58
  • 100
0
votes
3 answers

Given a Python string describing object.attribute, how do I separate the attributes's namespace from the attribute?

Given a Python string describing object.attribute, how do I separate the attributes's namespace from the attribute? Desired Examples: ns_attr_split("obj.attr") => ("obj", "attr") ns_attr_split("obj.arr[0]") => ("obj",…
Crazy Chenz
  • 12,650
  • 12
  • 50
  • 62
0
votes
1 answer

Jython 2.1 __getattr__

I am trying to implement a wrapper/proxy class for a java object (baseClient) in jython v2.1. Everything seems to be working ok except when the following statement is encountered: if __client != None # __client is an instance of the ClientProxy…
user213089
0
votes
2 answers

Python: Trying to set attributes of a class instance from output of another class instance

I have list of content block 'instances' which as the list is processed the content for each block is generated in an instance of a content type class (if that makes sense). As I iterate through the list I am trying to set the attributes of the…
user521836
0
votes
1 answer

Python: avoid __setattr__ loop using __dict__, shouldn't it loop anyway?

I know that you should be aware while redefine setattr method that you could end in a loop. I know there's two solution to avoid loops: 1) using object superclass, calling : object.__setattr__(instance,attr,value) 2) using the class…
Mattia
  • 47
  • 1
  • 9
0
votes
1 answer

Set a mongo database in pymongo without eval(). Maybe setattr()?

I'd been used to doing this in pymongo as a means of accessing a particular database: import pymongo connection = pymongo.MongoClient() db = connection.DBNAME then querying db.collectioname.find(), etc. However, I now want to be able to connect…
Mittenchops
  • 18,633
  • 33
  • 128
  • 246
0
votes
1 answer

how to set attribute to a python file?

I know how to set attribute for a class, but can't find how to set for a file.. flow = ['1','4','5','2'] def test_generator(test): def test_parser(self): print test return test_parser class API_tests(unittest.TestCase): …
eligro
  • 785
  • 3
  • 13
  • 23
0
votes
2 answers

__setattr__ breaking code?

Here's the code I have class Human(object): def __init__(self, name, gender): self.name = name self.gender = gender print 'Hi there, I am '+ self.name def ThankHeavens(self): return 'Thanks Gods!' class Soldier(Human): def…
andhudhow
  • 21
  • 1
  • 3
-1
votes
0 answers

The last call to `setattr` seems to override every previous call to it

When attempting to dynamically add functions to an object, I'm observing a weird situation. During the process, it seems to be working fine, as every function returns the expected value. But when calling each one of these functions again at the end…
bbbbbbbbb
  • 107
  • 2
-1
votes
1 answer

Python assigning the string to the variable - Str object is not callable message

I have the following problem where roles = models.StringField def role(player): if player.rank == 1: return 'leader' else: return 'member' Later I am trying to assign the role function outcome which is string to roles…
Arsil
  • 11
-1
votes
1 answer

Python reflection, getter to methods

class A: def __init__(self): self.var = 10 def print(self): print("hiiiiii") For an object of A, we can easily access the attributes with getattr and setattr. Is there any way in Python to have an instance of class and a…
-1
votes
1 answer

How to override getattr or getattribute in python to call gettattr or getattribute inside itself as in lazy recursive directory listing

For example something like this: class DotTabLoader(): def __init__(self, basedir, loaderfun): self._basedir = pathlib.PosixPath(basedir) self._loaderfun = loaderfun self._list = list(self._basedir.glob('*/')) …
mathtick
  • 6,487
  • 13
  • 56
  • 101
-1
votes
1 answer

Using __setattr__ with dictionary inside class

I want to define a class like this: class Test: _ID = itertools.count() def __init__(self): self.id = next(self._ID) self.__setattr__('_dict', {}) def __getattr__(self, key): return self._dict[key] def…
Jan Koval
  • 37
  • 1
  • 1
  • 4
-1
votes
1 answer

Using __setattr__ method with getattr in a class

I have a Python class as follows: class Base: def __init__(self, response=20): self.response = response def func1(self): self.response = 10 def func2(self): self.response = 20 def func3(self): …
Amistad
  • 7,100
  • 13
  • 48
  • 75
1 2 3
13
14