Python 如何从类选择器访问类学生的变量?
我希望Python 如何从类选择器访问类学生的变量?,python,python-3.x,oop,Python,Python 3.x,Oop,我希望st()返回学生的姓名 但是为什么我会得到空的列表 在讲师班级中,我如何获得学生班级的名称 class student: def __init__(self,*name): self.name=list(name) def stud(self): return self.name stu=student("david","nick") print(stu.stud()) class lector(student): def st
st()
返回学生的姓名
但是为什么我会得到空的列表
在讲师
班级中,我如何获得学生
班级的名称
class student:
def __init__(self,*name):
self.name=list(name)
def stud(self):
return self.name
stu=student("david","nick")
print(stu.stud())
class lector(student):
def st(self):
return self.name
lec=lector()
print(lec.st())
结果:
['david','nick']
[]
1)。类选择器中没有call()
函数。
2). 我已修改您的代码以获得结果:-
class student:
def __init__(self,*name):
student.name=list(name)
def stud(self):
return student.name
stu=student("david","nick")
print(stu.stud())
class lector(student):
def __init__(self): # Now you can access the variable which are inside student classs
super
def st(self):
return lector.name
lec=lector()
print(lec.st())
我希望它能对您有所帮助。classlector
没有定义call
方法lector
类没有多大意义。为什么它是学生
的子类?你的整个问题很难理解。如果你想得到这方面的帮助,你需要更好地解释你的任务。我认为您应该进一步学习Python教科书或教程,以便更好地了解类在Python中的工作方式。您通过调用lec=lector()
how edit code??否,将lector
的名称设置为[]