Introduction to classes

class Student(object):
    perc_raise = 1.05 
    def __init__(self, first, last, grade):
        self.first = first
        self.last =  last
        self.grade =  grade = first + "." + last + ""

    def student(self): 
        return "My name is {}. My last name is {}. My email address is {}. My +2 grade is {}".format(self.first,self.last,, self.grade)
    def apply_raise(self):
        self.grade = float(self.grade * 1.05 )

std_1 = Student("Bijay","Gurung", 21) 
std_2 = Student("Anna" , "Gurung", 90)

print("My aggregate garde is {}".format(std_2.grade))

In function apply_raise if I use perc_raise instead of 1.05 it shows error name ‘perc_raise’ is not defined. But perc_raise is an class variable that is shared by all instance/object of a class. Any Solution ?


yes, but to access class variable we need to do self.class_variable, class variable are automatically added to self.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.