Skip to main content
added explanation for the error message
Source Link
Aemyl
  • 2.4k
  • 1
  • 27
  • 53

You can use the classmethod decorator, which is more appropriate for this situation anyways:

class C: class Nested: counter = 0 @classmethod def increment(cls) -> int: cls.counter += 1 return cls.counter print(C().Nested.increment()) # prints 1 

If you are wondering why increment can't find Cl_static_parameter_nested in your example: you would have to write Cl_some_class.Cl_static_parameter_nested to access it from the global namespace.

You can use the classmethod decorator, which is more appropriate for this situation anyways:

class C: class Nested: counter = 0 @classmethod def increment(cls) -> int: cls.counter += 1 return cls.counter print(C().Nested.increment()) # prints 1 

You can use the classmethod decorator, which is more appropriate for this situation anyways:

class C: class Nested: counter = 0 @classmethod def increment(cls) -> int: cls.counter += 1 return cls.counter print(C().Nested.increment()) # prints 1 

If you are wondering why increment can't find Cl_static_parameter_nested in your example: you would have to write Cl_some_class.Cl_static_parameter_nested to access it from the global namespace.

Source Link
Aemyl
  • 2.4k
  • 1
  • 27
  • 53

You can use the classmethod decorator, which is more appropriate for this situation anyways:

class C: class Nested: counter = 0 @classmethod def increment(cls) -> int: cls.counter += 1 return cls.counter print(C().Nested.increment()) # prints 1