setattr

How to create a property with its name in a string?

Using Python I want to create a property in a class, but having the name of it in a string. Normally you do: blah = property(get_blah, set_blah, del_blah, "bleh blih") where get_, set_ and del_blah have been defined accordingly. I've tried to do the same with the name of the property in a variable, like this: setattr(self, "blah", pr...

Python __init__ setattr on arguments?

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 arguments into a list (without resorting to *args or **kwargs) and then using setattr to set the instance variables, with the name of the p...

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 class raise AttributeError(attr) is called in __getattr__(), because self.__baseClient does...

Overridden attribute access does not work (as expected)

The main objective of the following module, is to provide a kind of "constant" semantics for some names. class ConstantError(Exception): def __init__(self, msg): self._msg = msg class Constant(object): def __init__(self, name): self._name = name def __get__(self, instance, owner): ret...

Why does the "name" parameter to __setattr__ include the class, but __getattr__ doesn't?

The following code: class MyClass(): def test(self): self.__x = 0 def __setattr__(self, name, value): print name def __getattr__(self, name): print name raise AttributeError(name) x = MyClass() x.test() x.__y Outputs: _MyClass__x __y Traceback (most recent call last): ... AttributeError:...

Setting an class attribute with a given name in python while defining the class

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 can't seem to find the reference to the class to get the setattr to work the same as just assigning a variable in the class definition. ...

How do I call setattr() on the current module?

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 the same effect as: SOME_CONSTANT = 42 within the module containing these lines (with the correct object). I'm generate several values at th...

create a class attribute without going through __setattr__

Hello, 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 overriding __setattr__ I was having trouble creating that initial dicionary in the...