CSE 1010 Lecture Notes - Lecture 22: Local Variable, Init
WEEK 13 - LECTURE 22 - SELF
● Consider this class
○ When any method in a class is called, Python places the instance that the
method was called on into the self parameter variable.
○ Run it
■ This is the unique identifier for the instance of Person stored in the
variable p1.
○ The instance in p1 has a variable age attached to it. This is one way to see its
value.
■ You can't call a method without using an instance on the left.
■ We say that p1 is the receiver of the method call.
jadetortoise401 and 55 others unlocked
84
CSE 1010 Full Course Notes
Verified Note
84 documents
Document Summary
When any method in a class is called, python places the instance that the method was called on into the self parameter variable. This is the unique identifier for the instance of person stored in the variable p1. The instance in p1 has a variable age attached to it. You can"t call a method without using an instance on the left. We say that p1 is the receiver of the method call. The . getage() method returns the value of age attached to the receiver, which is the instance in the variable p1. When any method is called, the receiver is placed into the self parameter variable. I want to attach the value in age to the instance (somehow). This makes no sense: assign age to itself. But when the __init__ method finishes, the variable age goes away (age is a local variable). The variable is not attached to the instance.