0

I have nested classes because they are tightly related.

Now i want to have the following type of code

class one(Some-other-type):
    ...
    // More functions here

    class two(one):
       // Some more functions here.

The Inner class "two" type should be "one" but if i put that i get an error. The other way is to not make it nested like below

class one(Some-other-type):
    ...
    // More functions here

class two(one):
    // Some more functions here.

But then i dont want class "two" to be accessible when the module is imported. I dont really care about the functions offered by "one" but it needs to be under it for clarity of code.

Any thoughts?

mega-crazy
  • 838
  • 2
  • 17
  • 36
  • 1
    Possible duplicate of [Access outer class from inner class in python](http://stackoverflow.com/questions/2024566/access-outer-class-from-inner-class-in-python) – bhavya.work Nov 13 '16 at 01:46
  • 2
    "But then i dont want class "two" to be accessible when the module is imported." Python doesn't bother with access modifiers like `private`. Just put an underscore in front of `class _two` to indicate it's private. Because in the other case, the calss is always accessible anyway through `one.two`. –  Nov 13 '16 at 01:49
  • @Evert yea i guess thats the way to go – mega-crazy Nov 13 '16 at 02:01
  • 1
    Also, you could use `__all__ = ['one']` see [Importing a Package](https://docs.python.org/3.5/tutorial/modules.html#importing-from-a-package) – Bryce Guinta Nov 13 '16 at 02:07
  • @BryceGuinta that only affects `from somemodule import *` type imports, AFAIK. – juanpa.arrivillaga Nov 13 '16 at 02:23
  • If you are using nested classes in Python, you are almost certainly doing something wrong. – juanpa.arrivillaga Nov 13 '16 at 02:32
  • @juanpa.arrivillaga The only way I'm seeing a leading underscore different from `__all__` is that IPython autocomplete doesn't pick up leading underscore attributes. However you can directly access the 'hidden' attribute either way. The leading underscore is more explicit in that the attribute is not intended to be used outside of the module however. – Bryce Guinta Nov 13 '16 at 02:39
  • @juanpa.arrivillaga is it wrong in design perspective? – mega-crazy Nov 13 '16 at 03:38
  • @Anup Yes, I think so. – juanpa.arrivillaga Nov 13 '16 at 04:03

1 Answers1

0

You cannot refer to the class while it is being constructed. However you can refer to a class inside its own method since the method body isn't evaluated until after the class in constructed.

class one(object):
    @staticmethod
    def get_two():
        class two(one):
           pass
        return two()
    def f(self):
        print("I am %s" % type(self).__name__)

two inherits f from one

>>> obj = one.get_two()
>>> obj.f()
"I am two"
Bryce Guinta
  • 3,456
  • 1
  • 35
  • 36
  • A possibly significant downside to this is that each instance you get from `get_two` will be an instance of a different `two` class. That is, the classes will have the same name (and same methods, etc.), but they won't be the same class. `type(one.get_two()) != type(one.get_two())` – Blckknght Nov 13 '16 at 10:10
  • @Blckknght You're right. get_two() could return the class and you could just instantiate `two` objects from only that class type if that mattered. – Bryce Guinta Nov 13 '16 at 14:52