-3 python python-2.7
#C:/Python32
class Person:
def __init__(self, name = "joe" , age= 20 , salary=0):
self.name = name
self.age = age
self.salary = salary
def __printData__(self):
return " My name is {0}, my age is {1} , and my salary is {2}.".format(self.name, self.age, self.salary)
print(Person)
class Employee(Person):
def __init__(self, name, age , salary ):
Person. __init__ (self,name = "Mohamed" , age = 20 , salary = 100000)
def __printData__(self):
return " My name is {0}, my age is {1} , and my salary is {2}.".format(self.name, self.age, self.salary)
print(Employee)
p= Person()
e = Employee()
Run Code Online (Sandbox Code Playgroud)
您的问题可以简化为:
class Person:
print(Person)
Run Code Online (Sandbox Code Playgroud)
这将提出一个NameError.构造类时,将执行类的主体并将其放在特殊的命名空间中.然后type将该命名空间传递给负责实际创建该类的命名空间.
在你的代码中,你正在尝试print(Person)在Person实际创建类之前(在执行类的主体的阶段 - 在它被传递到type并绑定到类名之前),这导致了NameError.