80

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, my idea was to overwrite the __getattr__ and __setattr__ methods and to check, if the given name is device to redirect the return to self. But I encountered a problem giving infinite recursions. The example code is as follows:

class MyTest(object):
    def __init__(self, x):
        self.x = x

    def __setattr__(self, name, value):
        if name=="device":
            print "device test"
        else:
            setattr(self, name, value)

test = MyTest(1)

As in __init__ the code tried to create a new attribute x, it calls __setattr__, which again calls __setattr__ and so on. How do I need to change this code, that, in this case, a new attribute x of self is created, holding the value 1?

Or is there any better way to handle calls like instance.device.read to be 'mapped' to instance.read?

As there are always questions about the why: I need to create abstractions of xmlrpc calls, for which very easy methods like myxmlrpc.instance,device.read and similar can be created. I need to 'mock' this up to mimic such multi-dot-method calls.

talonmies
  • 70,661
  • 34
  • 192
  • 269
Alex
  • 41,580
  • 88
  • 260
  • 469

5 Answers5

85

You must call the parent class __setattr__ method:

class MyTest(object):

    def __init__(self, x):
        self.x = x

    def __setattr__(self, name, value):
        if name=="device":
            print "device test"
        else:
            super(MyTest, self).__setattr__(name, value)
            # in python3+ you can omit the arguments to super:
            #super().__setattr__(name, value)

Regarding the best-practice, since you plan to use this via xml-rpc I think this is probably better done inside the _dispatch method.

A quick and dirty way is to simply do:

class My(object):
    def __init__(self):
        self.device = self
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
  • 1
    I like the quick-and-dirty way: Just one line to get the desired behaviour! Thanks – Alex Jun 10 '13 at 09:04
  • This is also mentioned in the [Python docs](https://docs.python.org/3/reference/datamodel.html#object.__setattr__). – djvg Dec 07 '18 at 18:13
  • @Bakuriu would you please explain what this line is doing exactly? super(MyTest, self).__setattr__(name, value) – Shahryar Apr 01 '20 at 07:03
  • 1
    @Shahryar It is explained in the first line of my answer. It is calling the parent class `__setattr__` method. `super(Class, instance)` creates an object that is able to call the methods of the *parent class of `Class`* on `instance`. You could also do `object.__setattr__(instance, name, value)` in this case but `super` is able to handle correctly multi-inheritance and should be preferred. – Bakuriu Apr 04 '20 at 09:03
  • why can't we simply say self[name] = value? is it wrong? – Shahryar Apr 07 '20 at 14:02
  • 1
    @Shahryar Because that's not even valid python syntax. `self[name] = value` is equivalent to `self.__setitem__(name, value)` which most of the time is not implemented. And if you try to use the `setattr` builtin what would happen is that `setattr(self, name value)` will recursively call `__setattr__` and you end up in an infinite loop. – Bakuriu Apr 07 '20 at 16:29
31

Or you can modify self.__dict__ from inside __setattr__():

class SomeClass(object):

    def __setattr__(self, name, value):
        print(name, value)
        self.__dict__[name] = value

    def __init__(self, attr1, attr2):
        self.attr1 = attr1
        self.attr2 = attr2


sc = SomeClass(attr1=1, attr2=2)

sc.attr1 = 3
Vindolin
  • 827
  • 8
  • 12
  • Why does this not call `setattr(self, "__dict__", value)`, repeating the original infinite recursion problem? – Maxpm May 15 '19 at 06:26
  • 4
    Because this is not setting the `__dict__` attribute, gut getting it and then setting an item on the returned dict, i.e., it's calling sth like: `getattr(self, "__dict__").__setitem__(name, value)` – Se Norm May 22 '19 at 09:37
6

You can also use object.

class TestClass:
    def __init__(self):
            self.data = 'data'
    def __setattr__(self, name, value):
            print("Attempt to edit the attribute %s" %(name))
            object.__setattr__(self, name, value)
Pang
  • 9,564
  • 146
  • 81
  • 122
Danny Hpy
  • 71
  • 1
  • 5
4

or you can just use @property:

class MyTest(object):

    def __init__(self, x):
        self.x = x

    @property
    def device(self):
        return self
Aviad Rozenhek
  • 2,259
  • 3
  • 21
  • 42
3

If you don't want to specify which attributes can or cannot be set, you can split the class to delay the get/set hooks until after initialization:

class MyTest(object):
    def __init__(self, x):
        self.x = x
        self.__class__ = _MyTestWithHooks

class _MyTestWithHooks(MyTest):
    def __setattr__(self, name, value):
        ...
    def __getattr__(self, name):
        ...

if __name__ == '__main__':
    a = MyTest(12)
    ...

As noted in the code you'll want to instantiate MyTest, since instantiating _MyTestWithHooks will result in the same infinite recursion problem as before.

Fax
  • 384
  • 2
  • 9