Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在python中使用类方法的程序_Python 3.x_Loops_Class_Oop_Methods - Fatal编程技术网

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