Thread: Nested Classes

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2

    Nested Classes


    Hi guys.
    I'm new to python, so please bare with me.

    What's the right way to use nested classes?

    Code:
    class Outer:
        def __init__(self):
            #some code
        #some more methods
        #..
        #..
            class Inner:
                def __init__(self):
                    #some code
                    #some more methods
                    #..
                    #..
    Or maybe:

    Code:
    class Inner:
        def __init__(self):
            #some code
        #some more methods
        #..
        #..
    
    
    class Outer:
        def __init__(self):
            #some code
        #some more methods that creates
        #Inner class objects
        #..
        #..
    and also, some more tips and guidance for a newbie like me are always appreciated.

    Thanks in advanced!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    If Inner won't have any sort of general purpose, and if Inner is only useful to Outer, then you may as well define Inner in Outer.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    Hi, b49P23TIvg.
    Thanks again for the help.

    So basically both forms are OK, and it is just a matter of modularity and necessity.

IMN logo majestic logo threadwatch logo seochat tools logo