Correct syntax for nested classes in Python?

I am completely new to programming and still learning my ropes. Sorry if this question is too elementary.

It’s hard for me to clarify my question, so here is an example of what I want to accomplish: suppose a is an instance of class X and must have attributes a1, a2, a3, b1, b2, b3, c1, c2 and c3. I want to put a1 through a3, b1-b3 and c1-c3 into my own classes A, B, C, nested under X for ease of use. What will be the correct syntax?

 class X:
    def __init__ (self, name, A, B, C):
        self.name = name
        self.A = A
        self.B = B
        self.C = C
    class A (X):
       def _init_ (self, name, a1, a2, a3):
           self.name = name
           self.a1 = a1
           self.a2 = a2
           self.a3 = a3
    class B (x):
       def _init_ (self, name, b1, b2, b3):
           self.name = name
           self.b1 = a1
           self.b2 = a2
           self.b3 = a3
    class C (X):
       def _init_ (self, name, c1, c2, c3):
           self.name = name
           self.c1 = a1
           self.c2 = a2
           self.c3 = a3

Since it was entirely based on conjectures, I am 99.9% sure that this is wrong, but I’m not sure what I should do to make everything right. I tried to find the answers of the "nested classes", but the answers that I found did not really clarify my position.

, Python.

.

EDIT: , . .

+3
2

- , . , , , Python. , , ; : mixins/ .

mixin :

class A(object):
    def __init__(self, a=None, *args, **kwargs):
        self.a = a
        super(A, self).__init__(*args, **kwargs)

class B(object):
    def __init__(self, b=None, *args, **kwargs):
        self.b = b
        super(B, self).__init__(*args, **kwargs)

class X(A, B):
    def __init__(self, *args, **kwargs):
        super(X, self).__init__(*args, **kwargs)

x = X(a='foo', b='bar')
print x.a, x.b
  • X A, B, : class X(A, B).

  • __init__ *args, **kwargs ; super() X A B, , ( ) . A, B A.__init__, A.

  • super() Python.

, :

class A(object):
    def __init__(self, a):
        self.a = a

class B(object):
    def __init__(self, b):
        self.b = b

class X(object):
    def __init__(self, a, b):
        self.A = A(a)
        self.B = B(b)

x = X('foo', 'bar')
print x.A.a, x.B.b

, super() , . , X. , :

class X(object):
    # <etc>
    @property
    def a(self):
        return self.A.a

__getattr__ .

class X(object):
    # <etc>
    def __getattr__(self, item):
        for component in (getattr(self, c) for c in ['A', 'B']):
            if hasattr(component, item):
                return getattr(component, item)

- :

class X(object):
    def __init__(self, A, B):
        self.A = A
        self.B = B

x = X(A('foo'), B('bar'))
print x.A.a, x.B.b

X, - A B, . : A B , . factory :

def make_X(a, b):
    return X(
        A(a), 
        B(b)
    )
+2

.

, , , python . , a a

 class X:
    def __init__ (self, name, a, b, c):
        self.name = name
        self.a = a

class A (X):
   def _init_ (self, name, a1, a2, a3):
       self.name = name
       self.a1 = a1
       self.a2 = a2
       self.a3 = a3

class B (A):
   def _init_ (self, name, a1, a2, a3):
       self.name = name
       self.b1 = a1
       self.b2 = a2
       self.b3 = a3

class C (A):
   def _init_ (self, name, a1, a2, a3):
       self.name = name
       self.c1 = a1
       self.c2 = a2
       self.c3 = a3

,

+1

All Articles