![]() | ![]() |
Example from the previous class
Open the previous program file with class Bright
student1=Bright()
Discussion
The last program will create object student1, a new instance. The attributes of objects can be accessed via the specific object name prefix. The attributes can be a method or data including the matching class functions. In other terms, Bright.salute is a function object and student1.salute will be a method object.
Example
Start IDLE.
Navigate to the File menu and click New Window.
Type the following:
class Bright:
"Another class again!"
c = 20
def salute(self):
print('Hello')
student2 = Bright()
print(Bright.salute)
print(student2.salute)
student2.salute()
Discussion
You invoked the student2.salute() despite the parameter ‘self’ and it still worked without placing arguments. The reason for this phenomenon is because each time an object calls its method, the object itself is passed as the first argument. The implication is that student2.salute() translates into student2.salute(student2). It is the reason for the ‘self; name.