1

I have the following python classes:

parent.py

#! /usr/bin/env python3 class parent(): CONSTANT = 10 

child.py

#! /usr/bin/env python3 from parent import parent class child(parent): print(CONSTANT) def my_method(self): print(CONSTANT) 

But child cannot access CONSTANT.

Why doesn’t that work? Is there a way to access the parent’s class attributes (without transforming them all into instance attributes…)

4
  • You will need to read up on how methods work in Python – that needs to be def my_method(self): so you have a reference to the instance, and then you'll want print(self.CONSTANT). Commented Aug 30, 2022 at 8:11
  • in my_method you just have to use self.CONSTANT not just CONSTANT. Commented Aug 30, 2022 at 8:16
  • @AKX I’m losing my mind, I believe I tried print(self.CONSTANT) and it gave me a NameError, but it seems to work now 😄 Commented Aug 30, 2022 at 8:16
  • Ah I remember what was the problem, I was writing class attributes in child, so no self keyword there… But I guess I could just use parent.CONSTANT :D Commented Aug 30, 2022 at 12:11

3 Answers 3

2

It already inherited that. Test it without that print line(I've put both in a module, but that doesn't matter the error is from something else, I'm gonna talk about it next):

class parent: CONSTANT = 10 class child(parent): pass print(child.CONSTANT) # 10 

But why it throws NameError: name 'CONSTANT' is not defined in your example?

class keyword in Python is used to create classes which are instances of type type. In it's simplified version, it does the following:

  1. Python creates a namespace(an empty dictionary basically) and executes the body of the class in that namespace so that it will be populated with all methods and attributes and so on...

  2. Then calls the three-arguments form of type(). The result of this call is your class which is then assign to a symbol which is the name of your class.

The point is when the body of the class is being executed, it doesn't know anything about the parent namespace , likewise it doesn't know what is CONSTANT. So print(CONSTANT) is gonna throw a NameError exception.

regarding your comment: Since you already inherited that value, you can access it through both the instance and the class. both work:

class parent: CONSTANT = 10 class child(parent): def regular_method(self): print(self.CONSTANT) print(self.__class__.CONSTANT) print(super().CONSTANT) @classmethod def class_method(cls): print(cls.CONSTANT) obj = child() obj.regular_method() obj.class_method() 
Sign up to request clarification or add additional context in comments.

3 Comments

I’ve modified child.py a bit: I’d like to use the parent’s constant in the child’s methods
Nit: Not the global namespace, nor parent's namespace.
@AKX oh, why did I say global, I was talking about parents :) thanks for fixing typo
2

They are inherited.

class parent: CONSTANT = 10 class child(parent): pass print(child.CONSTANT) # 10 

Comments

1

You should encase your print into classmethod and reference attribute of class rather than global variable, consider following simple example

class Parent: CONSTANT = 10 class Child(Parent): @classmethod def print_constant(cls): print(cls.CONSTANT) Child.print_constant() 

gives output

10 

1 Comment

Doesn't need to be a classmethod – a regular method with self.CONSTANT would work fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.