CSE 1010 Lecture Notes - Lecture 22: Local Variable, Init

27 views3 pages
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.
Unlock document

This preview shows page 1 of the document.
Unlock all 3 pages and 3 million more documents.

Already have an account? Log in
jadetortoise401 and 55 others unlocked
CSE 1010 Full Course Notes
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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions