Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

358
Vistas
Python - reference inner class from other inner class

I am trying to reference an inner class from another inner class. I have tried both :

class Foo(object):

  class A(object):
    pass

  class B(object):
    other = A

and

class Foo(object):

  class A(object):
    pass

  class B(object):
    other = Foo.A

with respective results:

Traceback (most recent call last):
  File "python", line 1, in <module>
  File "python", line 6, in Foo
  File "python", line 7, in B
NameError: name 'A' is not defined

and

Traceback (most recent call last):
  File "python", line 1, in <module>
  File "python", line 6, in Foo
  File "python", line 7, in B
NameError: name 'Foo' is not defined

Is this possible?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

This is not possible, since everything you define in a class becomes a valid member only in an instance of that class, unless you define a method with @staticmethod, but there is no such property for a class.

So, this won't work either:

class Foo(object):
    x = 10

    class A(object):
        pass

    class B(object):
        other = x

This will work, but it is not what you intended:

class Foo(object):
  x = 10

  class A(object):
    pass

  class B(object):
    def __init__(self):
        self.other = Foo.A

f = Foo()
print(f.B().other)

The output is:

<class '__main__.Foo.A'>

The reason this works is that the methods (in this case __init__) are evaluated when the object is created, while assignment before the __init__ are evaluated while the class is read and interpreted.

You can get about the same thing you want by simply define all the classes inside a module of their own. The importing the module, makes it an object whose fields are the classes you define in it.

over 4 years ago · Santiago Trujillo Denunciar

0

I don't think it's good object oriented practice, but you can set inner class attributes at the outer class scope. For instance.

class Class2:

    class Labels:
        c2l1 = 'label 1'
        c2l2 = 'label 2' 

    class Params:
        pass 
        # p1 = None
        # p2 = None
        # p3 = None

    Params.p1 = Labels.c2l2
    Params.p2 = 1234


print(Class2.Params.p1)
print(Class2.Params.p2)
# print(Class2.Params.p3)

label 2
1234

These are all class attributes, but instance attributes should work similarly.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda