Python 3.x 在python中使用类方法的程序
获取此错误:Python 3.x 在python中使用类方法的程序,python-3.x,loops,class,oop,methods,Python 3.x,Loops,Class,Oop,Methods,获取此错误: class student: college = "tcet" def __init__(self,name,age,marks): self.name = name self.age = age self.marks = 90 @staticmethod #This is a StaticMethod Decorator de
class student:
college = "tcet"
def __init__(self,name,age,marks):
self.name = name
self.age = age
self.marks = 90
@staticmethod #This is a StaticMethod Decorator
def info():
return "This is data of students"
@classmethod #This is a ClassMethod Decorator
def college(cls):
return cls.college
s1 = student("shubham",19,100)
s2 = student("luffy",20,99.99)
print(student.college())
print(student.info())
方法只是Python中属性的另一种形式,因此当类属性
college
已经定义时,通过命名方法college
,方法对象将替换包含字符串的class属性college
您可以通过简单地以不同的方式命名属性来纠正此问题:
<bound method student.college of <class '__main__.student'>>
class student:
_college = "tcet"
@classmethod
def college(cls):
return cls._college