Round Robin
Round Robin

Reputation: 1

Why does my code show NameError in python when I try to use of OOP and classes?

def God():
    #code
    class Human:
        def __init__(self):
            pass
    class Man(Human):
        def __init__(self):
            pass       
    class Woman(Human):
        def __init__(self):
            pass   
    a=Man()
    b=Woman()

    return [a.__class__,b.__class__]

Why is this code showing NameError? I am trying to create three classes and return the instances of two classes as an entities in an array The test code is given below

paradise = God()
test.assert_equals(isinstance(paradise[0], Man) , True, "First object are a man")

Upvotes: 0

Views: 93

Answers (1)

Barmar
Barmar

Reputation: 780843

Man and Woman are local variables inside the God() function, you can't reference them outside the function. You should take the class definitions out of the function.

And if you want to return instances, don't use __class__.

class Human:
    def __init__(self):
        pass

class Man(Human):
    def __init__(self):
        pass   
    
class Woman(Human):
    def __init__(self):
        pass   

def God():
    a=Man()
    b=Woman()

    return [a ,b]

Upvotes: 1

Related Questions