bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Python/Object-Oriented Python
Python•Object-Oriented Python

Python self Parameter

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Python self Parameter?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Order

Put the learning moves in the order that makes the concept easiest to apply.

Accessing Properties with self
self Does Not Have to Be Named "self"
The self Parameter

The self Parameter

The self parameter is a reference to the current instance of the class.

It is used to access properties and methods that belong to the class.

self

Note

The self parameter must be the first parameter of any method in the class.

Why Use self?

Without self , Python would not know which object's properties you want to access:

self

self Does Not Have to Be Named "self"

It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any method in the class:

Example

class Person:
def __init__(myobject, name, age):
  myobject.name = name
  myobject.age = age
def greet(abc):
  print("Hello, my name is " + abc.name)
  p1 = Person("Emil", 36)
  p1.greet()

Note

While you can use a different name, it is strongly recommended to use self as it is the convention in Python and makes your code more readable to others.

Accessing Properties with self

You can access any property of the class using self :

self

Calling Methods with self

You can also call other methods within the class using self :

self

Previous

Python __init__() Method

Next

Python Class Properties