Introduction to classes


#1
class Student(object):
    
    perc_raise = 1.05 
    
    def __init__(self, first, last, grade):
        self.first = first
        self.last =  last
        self.grade =  grade
        self.email = first + "." + last + "@gmail.com"

    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.email, 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(std_2.student())
std_2.apply_raise()
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 ?


#2

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


#3

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