Show 3 minute read • Python 3.7—3.10 Watch as video 03:28
Sign in to your Python Morsels account to save your screencast settings. Don't have an account yet? Sign up here. When you define a class in Python, you'll see A Python class with self in each methodThe below
We can make an instance of this class, by calling it. We can access attributes and can call methods on this class instance:
Do we need that self argument?Each of the methods in our
Now when we call the
The error says
So whether you like it or not, the first argument to every one of your methods is going to be What is self?So what is Let's temporarily change our
Python's If we call the
If we look at the
That variable So But what does self mean? Could we rename it?What if we take
Would our code still work? If we make a new
From Python's perspective, it doesn't actually matter, what you call self is the first method of every Python classWhen Python calls a method in your class, it will pass in the actual instance of that class that you're working with as the first argument. Some programming languages use the word When you define a class in Python, every method that you define, must accept that
instance as its first argument (called The Series: ClassesClasses are a way to bundle functionality and state together. The terms "type" and "class" are interchangeable: You'll certainly use quite a few classes in Python (remember types are classes) but you may not need to create your own often. To track your progress on this Python Morsels topic trail, sign in or sign up. ✕ ↑ Concepts Beyond Intro to Python Intro to Python courses often skip over some fundamental Python concepts. Sign up below and I'll share ideas new Pythonistas often overlook. Apa fungsi self dalam python?Fungsi dari “self” ini sebenarnya adalah sebagai sebuah variabel saja yang yang menyatakan kelas itu sendiri.
Apa itu super ()?Fungsi super() digunakan untuk mengacu ke kelas induk dari suatu objek. Fungsi ini akan mengembalikan objek proksi (objek sementara superclass) yang memungkinkan kita untuk mengakses metode kelas dasar.
Apa itu init pada python?Init pada Python digunakan untuk konfigurasi object yang telah di-construct. Pada __init__ function umumnya dilakukan inisialisasi properties, atau melakukan operasi yang harus dilakukan sebelum object dibuat.
Apa itu class pada python?Kelas Pada Python
Apa itu sebuah kelas? Kelas atau class pada python bisa kita katakan sebagai sebuah blueprint (cetakan) dari objek (atau instance) yang ingin kita buat. Kelas adalah cetakannya atau definisinya, sedangkan objek (atau instance) adalah objek nyatanya.
|