One way to do this is to define subspace and another_subspace as properties that return objects that do_smth and do_smth_else respectively:
class Something: @property def subspace(self): class SubSpaceClass: def do_smth(other_self): print('do_smth') return SubSpaceClass() @property def another_subspace(self): class AnotherSubSpaceClass: def do_smth_else(other_self): print('do_smth_else') return AnotherSubSpaceClass()
What does what you want to do:
>>> smth = Something() >>> smth.subspace.do_smth() do_smth >>> smth.another_subspace.do_smth_else() do_smth_else
Depending on what you intend to use the methods on, you can make a SubSpaceClass singleton, but I doubt the performance gain is worth it.
Jonas adler
source share