Ad

Name 'Child1' Is Not Defined

- 1 answer

I am very new to python programming I have a code thats shows the below error

child_=Child1()
NameError: name 'Child1' is not defined

Here is the code that i have written in pycharm project

Parent1.py

class Parent1:
    value1="this is value1"
    value2="this is value2"
class Parent2:
    value3="this is value1"
    value4="this is value2"
class Child1(Parent1,Parent2):

    child_=Child1()
    print(child_.value1)
    print(child_.value2)
    print(child_.value3)
    print(child_.value4)

I have defined def before class of Child1 but still I couldn't able to solve the issue.

Ad

Answer

You cannot initialize a static class variable which references the class itself.

class Parent1:
    value1="this is value1"
    value2="this is value2"

class Parent2:
    value3="this is value1"
    value4="this is value2"

class Child1(Parent1,Parent2):
    pass

child_=Child1()
print(child_.value1)
print(child_.value2)
print(child_.value3)
print(child_.value4)
Ad
source: stackoverflow.com
Ad