0

This construct has some similarities with instance member class (known from Java, but available in Python as well). But instead of having a separate class created for each instance I think there may be some benefit of having the actual class being shared and instead just have a reference to the "owner" instance.

The construct looks something like:

def fubar(cls):
    def init(own, *args, **kwds):
        return cls(own, *args, **kwds)
    return init

class Outer:
    @fubar
    class Inner:
        def __init__(self, own, *args, **kwds):
            self._owner = own

Then if you have an instance o of Outer you would be able to create an instance of Inner by calling i=o.Inner(*args, **kwds) and i._owner would automagically be assigned to o.

Now does this construct/idiom have a recognized name? That is is there a proper name to be used instead of fubar?

skyking
  • 13,817
  • 1
  • 35
  • 57
  • 2
    What exactly is the purpose of this? The end result is that `Outer` has a function which, if called, instantiates a class but does not save a direct reference to instance, essentially using `Inner.__init__` solely for a side effect. Surely there is a clearer way of accomplishing whatever it is you are trying to accomplish. I would call this "obfuscated initialization". – chepner Nov 16 '16 at 14:09
  • @chepner I missed to return an instance to the `Inner` instance. The purpose is being able to call `o.Inner(*args, **kwds)` instead of `o.Inner(o, *args, **kwds)` that is make the call somewhat shorter (ie not having to repeat `o` twice in the call). – skyking Nov 16 '16 at 14:24

0 Answers0