setattr Questions

2

Solved

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.
Penelopepeneplain asked 26/9, 2011 at 17:57

6

Solved

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 th...
Medici asked 7/6, 2012 at 9:19

2

This question probably has an answer somewhere, but I couldn't find it. I'm using setattr to add a long list of descriptors to a class, and I have discovered that __set_name__ of the descriptor is ...
Operation asked 5/8, 2022 at 23:36

12

Solved

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 _...
Sociable asked 2/7, 2015 at 1:29

4

Solved

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__ __seta...
Prude asked 10/6, 2011 at 10:47

3

Solved

class A: __slots__ = ("a",) def __init__(self) -> None: self.a = 1 class B1: __slots__ = ("b",) def __init__(self, b) -> None: self.b = b def __getattr__(self, k)...
Metagalaxy asked 10/8, 2020 at 10:44

3

Solved

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 an...
Straw asked 12/8, 2011 at 15:10

6

Solved

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/func...
Markos asked 5/3, 2012 at 3:11

5

Solved

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, m...
Dooley asked 10/6, 2013 at 8:53

9

Solved

It seems that often __init__ methods are similar to this: def __init__(self, ivar1, ivar2, ivar3): self.ivar1 = ivar1 self.ivar2 = ivar2 self.ivar3 = ivar3 Is there someway to turn the argume...
Adamis asked 14/9, 2009 at 2:41

4

Solved

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...
Bromidic asked 21/10, 2012 at 14:52

2

Solved

I do understand how setattr() works in python, but my question is when i try to dynamically set an attribute and give it an unbound function as a value, so the attribute is a callable, the attribut...
Joo asked 19/3, 2019 at 12:38

3

Solved

I'm trying to copy functions from an arbitrary 'base' class into my new object. However, I'm getting the following error with this sample code. class my_base: def print_hey(): print("HEY") de...
Serotine asked 8/4, 2018 at 14:6

4

Solved

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 th...
Navarra asked 29/5, 2010 at 2:7

3

Solved

Suppose I have the following example: class foo: ... def bar(self, w, x, y, z, ...): self.w = w self.x = x self.y = y self.z = z ... I wish to reduce the n-number of attribute assignment ...
Kegan asked 9/9, 2016 at 15:28

6

Solved

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, *a...
Whichever asked 29/7, 2013 at 16:42

1

I'm trying to implement a dict-like object which can be accessed/modified with __getattr__ and __setattr__ for ease of use for my users. The class also implements some other simple functionality. ...
Eonism asked 2/11, 2017 at 17:32

4

Solved

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...
Himelman asked 22/5, 2013 at 20:24

2

Solved

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 t...
Fae asked 6/10, 2013 at 5:3

4

Solved

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 c...
Spurt asked 25/3, 2010 at 22:14

2

Solved

I have a boiler platey class that delegates some actions to a reference class. It looks like this: class MyClass(): def __init__(self, someClass): self.refClass = someClass def action1(self)...
Harlanharland asked 28/9, 2015 at 17:7

1

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 ...
Synonymous asked 22/7, 2013 at 9:35

1

Solved

Say I have a class which defines __slots__: class Foo(object): __slots__ = ['x'] def __init__(self, x=1): self.x = x # will the following work? def __setattr__(self, key, value): if key ==...
Icebound asked 24/10, 2013 at 12:49

4

Solved

There are a lot of good getattr()-like functions for parsing nested dictionary structures, such as: Finding a key recursively in a dictionary Suppose I have a python dictionary , many nests...
Eran asked 31/7, 2013 at 20:37

2

Solved

As illustrated in the code below, why can't I use __setattr__ to set values on a dict that is part of the class that overloads the method? I expected that b.hello would not exist. class MyClass():...
Sedentary asked 23/4, 2013 at 13:57

© 2022 - 2024 — McMap. All rights reserved.