PythonPython · Chapterبەش 11 · Lectureوانە 11.1

Classes and Objectsکلاسەکان و ئۆبجێکتەکان

Why programs are built from objects, the four big ideas of object-oriented programming at a glance, a class as the plan and objects as its instances, class, __init__ and self, attributes on each object, type() and isinstance(), variables as references to objects, a first method, printing an object, and the mistakes every beginner makes, such as forgetting self.

بۆچی لە دروست دەکرێن، چوار بیرۆکە گەورەکەی بە کورتی، وەک نەخشە و وەک ، class، __init__ و self، هەر ، type() و isinstance()، وەک بۆ ، یەکەم ، چاپکردنی و ئەو هەڵانەی هەموو تازەکارێک دەیانکات، وەک لەبیرچوونی self.

21 min read21 خولەک خوێندنەوە · 20 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Explain how object-oriented programming differs from procedural programming, and name its four big ideas.
  • Write a class with __init__, make several objects (instances) from it, and read and change their attributes.
  • Explain what self is and why aram.describe() means Student.describe(aram).
  • Check an object's class with type() and isinstance().
  • Explain that a variable holds a reference to an object, and predict what happens when two names share one object.
  • Write a first method and a __str__ method, and fix the common errors such as a missing self.
  • ڕوون بکەیتەوە چ جیاوازییەکی لەگەڵ هەیە و ناوی چوار بیرۆکە گەورەکەی بهێنیت.
  • بە __init__ بنووسیت، چەند () لێی دروست بکەیت و بخوێنیتەوە و بیانگۆڕیت.
  • ڕوون بکەیتەوە self چییە و بۆچی aram.describe() واتە Student.describe(aram).
  • بە type() و isinstance() بپشکنیت.
  • ڕوون بکەیتەوە کە بۆ هەڵدەگرێت و پێشبینی بکەیت چی ڕوو دەدات کاتێک دوو ناو یەک بەش دەکەن.
  • یەکەم و __str__ بنووسیت و هەڵە باوەکان چاک بکەیت، وەک نەبوونی self.

Why objects?بۆچی ئۆبجێکتەکان؟

Until now your programs kept their data in variables, lists and dictionaries, and their actions in functions. A marks program might keep each student in a dictionary and have separate functions such as average(student) and describe(student). This style is called procedural programming: the program is a series of steps grouped into functions.

تا ئێستا لە ، و هەڵدەگرت و کارەکانیان لە . نمرە لەوانەیە هەر قوتابییەک لە هەڵبگرێت و چەند جیای وەک average(student) و describe(student)ـی هەبێت. بەم شێوازە دەگوترێت : زنجیرەیەک هەنگاوە کە لە کۆ کراونەتەوە.

This works well for small programs. As a program grows, the data and the functions drift apart: any function can change any dictionary, a typo in a key such as "mraks" is found only when that line runs, and nothing says which functions belong to which data. Object-oriented programming (OOP) puts each piece of data together with the actions that use it, in one unit called an object. Python itself is object-oriented: every value you have used, from 5 to "Aram", is already an object.

ئەمە بۆ بچووک باش کار دەکات. کاتێک گەورە دەبێت، و لە یەک دوور دەکەونەوە: هەر دەتوانێت هەر بگۆڕێت، هەڵەیەکی نووسین لە وەک "mraks" تەنیا کاتێک دەردەکەوێت کە ئەو دێڕە جێبەجێ دەبێت و هیچ شتێک نالێت کام هی کام . ⁦(OOP)⁩ هەر پارچە لەگەڵ ئەو کارانەی بەکاری دەهێنن لە یەک یەکەدا کۆ دەکاتەوە کە پێی دەگوترێت . خۆی : هەموو ئەو تا ئێستا بەکارت هێناون، لە 5ـەوە تا "Aram"، پێشتر .

Two panels. Left, procedural programming: three blue boxes aram, shilan and karwan, each holding name, grade and marks, and three orange function boxes average(s), describe(s) and promote(s), with dashed lines from every function to every box. Bottom: data here, functions there. Right, object-oriented programming: two object cards, aram: Student and shilan: Student, each with its own name, grade and marks and, below a line, its own methods average() and describe(). Bottom: each object keeps its data and its actions.دوو بەش. چەپ، بەرنامەسازیی ڕێکاری: سێ چوارگۆشەی شین aram، shilan و karwan کە هەریەکەیان name، grade و marks هەڵدەگرێت و سێ چوارگۆشەی پرتەقاڵیی کردار: average(s)، describe(s) و promote(s)، بە هێڵی پچڕپچڕ لە هەموو کردارێکەوە بۆ هەموو چوارگۆشەیەک. خوارەوە: دراوە لێرە، کردارەکان لەوێ. ڕاست، بەرنامەسازیی تەنخواز: دوو کارتی ئۆبجێکت، aram: Student و shilan: Student، هەریەکەیان name، grade و marksـی خۆی هەیە و لە ژێر هێڵێکدا میتۆدەکانی خۆی average() و describe(). خوارەوە: هەر ئۆبجێکتێک دراوەی خۆی و کارەکانی خۆی هەڵدەگرێت.
Procedural: data and functions apart. Object-oriented: each object keeps both.: و جیا. : هەر هەردووکیان هەڵدەگرێت.CêIT

Why objects?بۆچی ئۆبجێکتەکان؟

One program, two stylesیەک بەرنامە، دوو شێواز

Here is the same small marks program twice. First the procedural version, which you can already write:

ئەمە هەمان بچووکی نمرەیە بە دوو شێوە. یەکەم، شێوە ڕێکارییەکە کە پێشتر دەتوانیت بینووسیت:

Procedural: dictionaries and functionsڕێکاری: فەرهەنگەکان و کردارەکانprocedural.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
# Procedural style: the data in dictionaries, the functions apart
aram = {"name": "Aram", "grade": 10, "marks": [80, 92, 75]}
shilan = {"name": "Shilan", "grade": 11, "marks": [95, 88, 91]}

def average(student):
    return sum(student["marks"]) / len(student["marks"])

def describe(student):
    return f"{student['name']} (grade {student['grade']})"

for s in [aram, shilan]:
    print(describe(s), "average:", round(average(s), 1))

Now the object-oriented version. Do not worry about the new words yet: class, __init__ and self are explained one by one in this lesson. Look at the shape: the data and the two actions now live together inside Student, and you ask each student object for its own average:

ئێستا شێوە . هێشتا خەمی وشە نوێیەکان مەخۆ: class، __init__ و self یەک بە یەک لەم وانەیەدا ڕوون دەکرێنەوە. سەیری شێوەکەی بکە: و هەردوو کارەکە ئێستا پێکەوە لە ناو Studentـدان و لە هەر قوتابی تێکڕای خۆی دەپرسیت:

Object-oriented: one Student classتەنخواز: یەک کلاسی Studentoop_preview.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
# Object-oriented style: each student keeps its data and its actions
class Student:
    def __init__(self, name, grade, marks):
        self.name = name
        self.grade = grade
        self.marks = marks

    def average(self):
        return sum(self.marks) / len(self.marks)

    def describe(self):
        return f"{self.name} (grade {self.grade})"

aram = Student("Aram", 10, [80, 92, 75])
shilan = Student("Shilan", 11, [95, 88, 91])

for s in [aram, shilan]:
    print(s.describe(), "average:", round(s.average(), 1))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-12

The class: the data (name, grade, marks) and the actions (average, describe) in one place.

: (name، grade، marks) و کارەکان (average، describe) لە یەک شوێندا.

Lineدێڕی 18

s.average() asks the object s for its own average.

s.average() تێکڕای خودی s دەپرسێت.

Both print the same lines. The difference shows in bigger programs: a Student object cannot be made without a name, a grade and marks, and all the code that works on students is in one place. Where Python stands among other languages is in Programming languages.

هەردووکیان هەمان دێڕەکان چاپ دەکەن. جیاوازییەکە لە گەورەتردا دەردەکەوێت: Student بەبێ ناو، پۆل و نمرە دروست ناکرێت و هەموو ئەو کۆدەی لەسەر قوتابییەکان کار دەکات لە یەک شوێندایە. لە نێوان زمانەکانی تردا لە کوێیە، لە زمانەکانی بەرنامەسازیدایە.

Four big ideas at a glanceچوار بیرۆکەی گەورە بە کورتی

Object-oriented programming rests on four ideas. You meet each of them properly later; here is the map:

لەسەر چوار بیرۆکە وەستاوە. هەریەکەیان دواتر بە تەواوی دەناسیت؛ ئەمە نەخشەکەیە:

  • Encapsulation: an object keeps its data and the methods that use it together, and other code changes the data only in safe ways, through the methods — like a shop's cash box: customers pay through the shopkeeper. Encapsulation and properties.
  • Abstraction: you use an object through a few simple actions without knowing how it works inside, as you use a phone's buttons without knowing its circuits. marks.sort() sorts without showing you how.
  • Inheritance: a new class is built from an existing one and gets its data and methods, then adds or changes some: a Student and a Teacher can both be built from a Person. Inheritance and polymorphism.
  • Polymorphism: one call works on different kinds of objects, each in its own way: len() counts the letters of a string and the items of a list.
  • : خۆی و ئەو بەکاری دەهێنن پێکەوە هەڵدەگرێت و کۆدی تر تەنیا بە شێوەی سەلامەت، لە ڕێگەی ، دەگۆڕێت — وەک سندووقی پارەی دووکانێک: کڕیار لە ڕێگەی دووکاندارەکەوە پارە دەدات. کەپسوولکردن و تایبەتمەندییەکان.
  • : لە ڕێگەی چەند کارێکی سادەوە بەکار دەهێنیت بەبێ ئەوەی بزانیت لە ناوەوە چۆن کار دەکات، وەک چۆن دوگمەکانی مۆبایل بەکار دەهێنیت بەبێ ئەوەی ئەلیکترۆنییەکەی بزانیت. marks.sort() ڕیزی دەکات بەبێ ئەوەی پیشانت بدات چۆن.
  • : نوێ لە هەبوو دروست دەکرێت و و ئەو وەردەگرێت، پاشان هەندێکیان زیاد دەکات یان دەیانگۆڕێت: Student و Teacher هەردووکیان دەتوانن لە Person دروست بکرێن. بۆماوە و فرەشێوەیی.
  • : یەک بانگکردن لەسەر چەند جۆرێکی کار دەکات و هەریەکەیان بە شێوەی خۆی: len() پیتەکانی و دانەکانی دەژمێرێت.
Four tiles. Encapsulation: a capsule holding marks [72, 90] that is reached only through an add() method; data and methods together, the data changes only through methods. Abstraction: a phone with a screen and three buttons, and behind a dashed wall three hidden gears; use a few simple actions, how they work stays hidden. Inheritance: Student and Teacher both point up to Person; a new class gets what an existing class has. Polymorphism: len("Aram") gives 4 letters and len([80, 92, 75]) gives 3 items; one call works on many kinds, each object answers its own way.چوار خانە. کەپسوولکردن: کەپسوولێک کە marks [72, 90] هەڵدەگرێت و تەنیا لە ڕێگەی میتۆدی add()ـەوە پێی دەگەیت؛ دراوە و میتۆدەکان پێکەوە و دراوەکە تەنیا لە ڕێگەی میتۆدەکانەوە دەگۆڕێت. ڕووتکردنەوە: مۆبایلێک بە شاشەیەک و سێ دوگمە و لە پشت دیوارێکی پچڕپچڕەوە سێ چەرخی شاراوە؛ چەند کارێکی سادە بەکار بهێنە، چۆنیەتی کارکردنیان شاراوەیە. بۆماوە: Student و Teacher هەردووکیان ئاماژە بۆ Person دەکەن؛ کلاسێکی نوێ ئەوەی وەردەگرێت کە کلاسێکی هەبوو هەیەتی. فرەشێوەیی: len("Aram") 4 پیت و len([80, 92, 75]) 3 دانە دەدات؛ یەک بانگکردن لەسەر چەند جۆرێک کار دەکات.
The four ideas of object-oriented programming, each with a small picture.چوار بیرۆکەکەی ، هەریەکەیان بە وێنەیەکی بچووکەوە.CêIT

A class is the plan; objects are made from itکلاس نەخشەکەیە؛ ئۆبجێکتەکان لێیەوە دروست دەکرێن

A class describes a kind of thing: which data every thing of that kind has, and what it can do. A class is not a thing itself. An object is one real thing made from the class, with its own values. One Student class can make the objects aram, shilan and karwan; each has its own name and grade.

جۆرێک شت وەسف دەکات: هەموو شتێکی ئەو جۆرە چ هەیە و دەتوانێت چی بکات. خۆی شت نییە. یەک شتی ڕاستەقینەیە کە لە دروست کراوە و خۆی هەیە. یەک Student دەتوانێت aram، shilan و karwan دروست بکات؛ هەریەکەیان ناو و پۆلی خۆی هەیە.

A shop's order formفۆڕمی داواکاریی دووکانێک

A shop in the bazaar prints one order form with empty lines: Name, Item, Amount. That printed form is the class. Every customer gets a copy and fills it in: Aram writes tea, 2 kg; Shilan writes rice, 5 kg. Each filled form is an object. The shop designs the form once but fills in hundreds, and a change on one customer's form does not change the others.

دووکانێک لە بازاڕدا یەک فۆڕمی داواکاری بە دێڕی بەتاڵەوە چاپ دەکات: ناو، کاڵا، بڕ. ئەو فۆڕمە چاپکراوە . هەر کڕیارێک کۆپییەک وەردەگرێت و پڕی دەکاتەوە: ئارام دەنووسێت چا، 2 کگم؛ شیلان دەنووسێت برنج، 5 کگم. هەر فۆڕمێکی پڕکراو . دووکانەکە یەک جار فۆڕمەکە دیزاین دەکات، بەڵام سەدان دانە پڕ دەکاتەوە و گۆڕینی فۆڕمی کڕیارێک فۆڕمەکانی تر ناگۆڕێت.

Where the analogy stops workingسنووری ئەم نموونەیە

A paper form only holds values. A class also has actions (methods) and can check the values it is given, which paper cannot do.

فۆڕمی کاغەز تەنیا هەڵدەگرێت. کاریشی () هەیە و دەتوانێت ئەو پێی دەدرێن بپشکنێت، کە کاغەز ناتوانێت.

Left: a blank order form with the lines Name, Item and Amount, labelled class Order: printed once. An arrow marked fill in leads to three filled forms: Aram, tea, 2 kg; Shilan, rice, 5 kg; Karwan, honey, 1 kg. Under each filled form is the code that would make it, such as Order("Aram", "tea", 2). Label: objects (instances), each with its own values. Bottom: changing one filled form does not change the others or the printed form.چەپ: فۆڕمێکی داواکاریی بەتاڵ بە دێڕەکانی ناو، کاڵا و بڕ، بە ناونیشانی class Order: یەک جار چاپ دەکرێت. تیرێک بە ناوی پڕکردنەوە دەچێت بۆ سێ فۆڕمی پڕکراو: ئارام، چا، 2 کگم؛ شیلان، برنج، 5 کگم؛ کاروان، هەنگوین، 1 کگم. لە ژێر هەر فۆڕمێکدا ئەو کۆدەیە کە دروستی دەکات، وەک Order("Aram", "tea", 2). ناونیشان: ئۆبجێکتەکان (نموونەکان)، هەریەکەیان بە نرخەکانی خۆی. خوارەوە: گۆڕینی فۆڕمێکی پڕکراو فۆڕمەکانی تر و فۆڕمە چاپکراوەکە ناگۆڕێت.
The printed form is the class; each filled-in copy is an object with its own values.فۆڕمە چاپکراوەکە ؛ هەر کۆپییەکی پڕکراو بە خۆیەوە.CêIT

A class is the plan; objects are made from itکلاس نەخشەکەیە؛ ئۆبجێکتەکان لێیەوە دروست دەکرێن

Instances: you have used them all alongنموونەکان: هەمیشە بەکارت هێناون

An object made from a class is called an instance of that class, and making it is called instantiation. Every value in Python is an instance of some class. type() shows which one, and the methods you already call, such as upper() and append(), belong to those classes:

کە لە دروست دەکرێت پێی دەگوترێت ئەو و دروستکردنەکەی پێی دەگوترێت instantiation. هەموو لە . type() پیشان دەدات کامیان و ئەو پێشتر بانگت کردوون، وەک upper() و append()، هی ئەو :

Values you know are instancesئەو نرخانەی دەیانناسیت نموونەنbuiltin_classes.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print(type(2026))
print(type("Hana"))
print(type([80, 92]))

name = "shilan"
print(name.upper())      # upper() is a method of every str object

marks = [80, 92]
marks.append(75)         # append() is a method of every list object
print(marks)

So "shilan" is an instance of the class str, and marks is an instance of list. In this lesson you write your own classes, like Student, and make instances of them in the same way.

کەواتە "shilan" strـە و marks listـە. لەم وانەیەدا خۆت دەنووسیت (وەک Student) و بە هەمان شێوە لێ دروست دەکەیت.

Four class boxes: class int with the instances 2026 and -5; class str with "Hana" and "shilan"; class list with [80, 92] and []; and, apart, your own class Student with the instances aram and shilan. The first three are built in and come with Python.چوار چوارگۆشەی کلاس: class int بە نموونەکانی 2026 و -5؛ class str بە "Hana" و "shilan"؛ class list بە [80, 92] و []؛ و جیا لەوان کلاسی خۆت Student بە نموونەکانی aram و shilan. سێ یەکەمیان ئامادەن و لەگەڵ Pythonـدا دێن.
Every value is an instance of a class: built-in classes and your own.هەموو : ئامادە و خۆت.CêIT

Your first classیەکەم کلاست

A class starts with the keyword class, a name and a colon; its body is indented. Python's style guide (PEP 8) writes class names in CapWords: Student, ExamResult. The smallest class has only pass in its body. To make an object, call the class like a function, with brackets:

بە class، ناوێک و دوو خاڵ دەست پێ دەکات؛ لاشەکەی بە دەنووسرێت. ڕێبەری شێوازی ⁦(PEP 8)⁩ ناوی بە CapWords دەنووسێت: Student، ExamResult. بچووکترین تەنیا passـی لە لاشەکەیدایە. بۆ دروستکردنی ، بانگ بکە وەک ، بە کەوانەوە:

An empty class and two objectsکلاسێکی بەتاڵ و دوو ئۆبجێکتfirst_class.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Student:
    pass                 # an empty class: no data, no actions yet

aram = Student()         # call the class to make an object
shilan = Student()       # a second, separate object

print(aram)
print(type(aram))
print(aram is shilan)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4-5

Each call Student() makes a new, separate object.

هەر بانگکردنێکی Student() نوێ و جیا دروست دەکات.

print(aram) shows the class (Student, written in the main program __main__) and a memory address. The two calls made two different objects, so aram is shilan is False. This class is still empty; next it gets data.

print(aram) (Student، کە لە سەرەکی __main__ـدا نووسراوە) و پیشان دەدات. دوو بانگکردنەکە دوو جیاوازیان دروست کرد، بۆیە aram is shilan Falseـە. ئەم هێشتا بەتاڵە؛ دواتر وەردەگرێت.

The code class Student: with pass on the next line, indented, and aram = Student() below. Labels: class is the keyword, Student is the name in CapWords, the colon ends the first line, the indented body follows. An arrow from Student() leads to a new object card aram: Student with no attributes yet. Bottom: PEP 8 writes class names in CapWords, such as Student or ExamResult.کۆدی class Student: بە pass لە دێڕی دواتردا بە بۆشایی سەرەتای دێڕ و aram = Student() لە خوارەوە. ناونیشانەکان: class وشە کلیلەییەکەیە، Student ناوەکەیە بە CapWords، دوو خاڵ دێڕی یەکەم تەواو دەکات و لاشەی بە بۆشایی سەرەتای دێڕ دوای دێت. تیرێک لە Student()ـەوە دەچێت بۆ کارتی ئۆبجێکتێکی نوێ aram: Student کە هێشتا خەسڵەتی نییە. خوارەوە: PEP 8 ناوی کلاسەکان بە CapWords دەنووسێت، وەک Student یان ExamResult.
The parts of a class statement, and the object that calling the class makes.بەشەکانی class و ئەو بانگکردنی دروستی دەکات.CêIT

__init__: filling in the form__init__: پڕکردنەوەی فۆڕمەکە

Most objects need values from the start: a student needs a name and a grade. A class receives them in a special method called __init__ (two underscores on each side). Python runs it by itself every time you make an object and passes in the values you gave. It is usually called the class's constructor; strictly it is an initialiser: the empty object already exists when __init__ runs, and __init__ fills it in:

زۆربەی لە سەرەتاوە پێویستیان بە هەیە: قوتابی پێویستی بە ناو و پۆل هەیە. ئەمانە لە تایبەتدا بە ناوی __init__ وەردەگرێت (دوو هێڵی ژێرەوە لە هەر لایەک). خۆی هەموو جارێک کە دروست دەکەیت جێبەجێی دەکات و ئەو داوتن دەینێرێتە ناوی. زۆربەی جار پێی دەگوترێت ؛ بە وردی دەستپێکەرە: بەتاڵەکە پێش کارکردنی __init__ هەیە و __init__ پڕی دەکاتەوە:

Every student gets a name and a gradeهەر قوتابییەک ناو و پۆلێک وەردەگرێتinit_self.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Student:
    def __init__(self, name, grade):
        self.name = name         # store the value in this object
        self.grade = grade

aram = Student("Aram", 10)
shilan = Student("Shilan", 11)

print(aram.name, aram.grade)
print(shilan.name, shilan.grade)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

__init__ has a first parameter self, then the values you pass: name and grade.

__init__ یەکەم selfـە، پاشان ئەو دەیاننێریت: name و grade.

Lineدێڕی 3-4

self.name = name stores the value inside this object, as its attribute name.

self.name = name لە ناو ئەم وەک name هەڵدەگرێت.

Student("Aram", 10) works in steps: Python makes a new, empty Student object, calls __init__ with self = that new object, name = "Aram" and grade = 10, and finally gives the finished object back, which aram = stores. The animation shows the steps.

Student("Aram", 10) هەنگاو بە هەنگاو کار دەکات: نوێ و بەتاڵی Student دروست دەکات، __init__ بانگ دەکات بە self = ئەو نوێیە، name = "Aram" و grade = 10، لە کۆتاییدا تەواوکراوەکە دەگەڕێنێتەوە و aram = هەڵیدەگرێت. ئەنیمەیشنەکە هەنگاوەکان پیشان دەدات.

Animation in six frames. Left: the line aram = Student("Aram", 10) and below it the class with def __init__(self, name, grade), self.name = name and self.grade = grade; the line that runs is highlighted. Right, in memory: 1, the call; 2, an empty Student object appears; 3, __init__ runs and self points to that new object, with name = "Aram" and grade = 10; 4, the row name "Aram" appears in the object; 5, the row grade 10 appears; 6, the name aram points to the finished object.
Animation in six frames. Left: the line aram = Student("Aram", 10) and below it the class with def __init__(self, name, grade), self.name = name and self.grade = grade; the line that runs is highlighted. Right, in memory: 1, the call; 2, an empty Student object appears; 3, __init__ runs and self points to that new object, with name = "Aram" and grade = 10; 4, the row name "Aram" appears in the object; 5, the row grade 10 appears; 6, the name aram points to the finished object.
What Student("Aram", 10) does, step by step.Student("Aram", 10) هەنگاو بە هەنگاو چی دەکات.CêIT

__init__: filling in the form__init__: پڕکردنەوەی فۆڕمەکە

What self really isself لە ڕاستیدا چییە

self is simply the first parameter of every method in a class. When you call a method through an object, Python puts that object into self for you. So aram.describe() means exactly Student.describe(aram): inside the method, self.name is Aram's name. Called through shilan, the same code reads Shilan's name.

self تەنیا یەکەم هەموو ناو . کاتێک لە ڕێگەی بانگ دەکەیت، خۆی ئەو دەخاتە ناو self. کەواتە aram.describe() ڕێک واتە Student.describe(aram): لە ناو self.name ناوی ئارامە. ئەگەر لە ڕێگەی shilanـەوە بانگ بکرێت، هەمان کۆد ناوی شیلان دەخوێنێتەوە.

self is the object before the dotself ئەو ئۆبجێکتەیە کە پێش خاڵەکەیەwhat_is_self.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

    def describe(self):
        return f"{self.name} is in grade {self.grade}"

    def my_id(self):
        return id(self)

aram = Student("Aram", 10)
print(aram.describe())
print(Student.describe(aram))     # the same call, written out in full
print(aram.my_id() == id(aram))   # self is the object aram itself
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 14

Written out in full: the class, the method, and the object as the first argument.

بە تەواوی نووسراو: ، و وەک یەکەم .

Lineدێڕی 15

id() gives an object's identity number: self and aram are one object.

id() ژمارەی ناسنامەی دەدات: self و aram یەک .

The name self is a convention, not a keyword: Python would accept another name, but every Python programmer writes self (PEP 8 asks for it), so always use it. Java does the same job with the keyword this: this in Java.

ناوی self ڕێککەوتنێکە، نەک : ناوێکی تریش قبووڵ دەکات، بەڵام هەموو self دەنووسێت (⁦PEP 8⁩ داوای دەکات)، بۆیە هەمیشە ئەوە بەکار بهێنە. هەمان کار بە this دەکات: this لە Java.

aram.describe() at the top; an arrow marked Python calls leads to Student.describe(aram); a curved arrow leads from that to def describe(self):, and another from there to the object card aram: Student with name "Aram" and grade 10. Labels: aram goes into self; self.name is "Aram".aram.describe() لە سەرەوە؛ تیرێک بە ناوی Python بانگ دەکات دەچێت بۆ Student.describe(aram)؛ تیرێکی چەماوە لەوێوە دەچێت بۆ def describe(self): و یەکێکی تر لەوێوە بۆ کارتی ئۆبجێکتی aram: Student بە name "Aram" و grade 10. ناونیشانەکان: aram دەچێتە ناو self؛ self.name واتە "Aram".
aram.describe() is Student.describe(aram): the object before the dot becomes self.aram.describe() هەمان Student.describe(aram)ـە: ئەو پێش خاڵەکەیە دەبێتە self.CêIT

Attributes: each object's own dataخەسڵەتەکان: دراوەی تایبەتی هەر ئۆبجێکتێک

A value stored in an object, such as aram.name, is an attribute. Values made with self. in __init__ are instance attributes: every object has its own. Read or change one with the object, a dot and the attribute's name. vars() shows all the attributes of an object as a dictionary:

کە لە هەڵگیراوە، وەک aram.name، . ئەو لە __init__ـدا بە self. دروست دەکرێن ن: هەر هی خۆی هەیە. بە ، خاڵێک و ناوی دەیخوێنیتەوە یان دەیگۆڕیت. vars() هەموو وەک پیشان دەدات:

Two objects, two sets of attributesدوو ئۆبجێکت، دوو کۆمەڵ خەسڵەتattributes.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

aram = Student("Aram", 10)
shilan = Student("Shilan", 12)

aram.grade = 11                  # change one object's attribute
print(aram.grade, shilan.grade)
print(vars(aram))                # every attribute of aram
print(vars(shilan))

Changing aram.grade did not touch shilan.grade: two objects, two sets of attributes. Python even lets you add an attribute later (aram.phone = ...), but good classes make every attribute in __init__, so all objects of the class have the same shape.

گۆڕینی aram.grade دەستی لە shilan.grade نەدا: دوو ، دوو کۆمەڵ . تەنانەت ڕێگە دەدات دواتر زیاد بکەیت (aram.phone = ...)، بەڵام باش هەموو لە __init__ـدا دروست دەکات، بۆیە هەموو ئەو هەمان شێوەیان هەیە.

aram.grade = 11 with an arrow into the card aram: Student, whose row grade 11 is highlighted; next to it the card shilan: Student with name "Shilan" and grade 12, labelled not changed. Below: vars(aram) gives {'name': 'Aram', 'grade': 11} and vars(shilan) gives {'name': 'Shilan', 'grade': 12}.aram.grade = 11 بە تیرێک بۆ ناو کارتی aram: Student کە ڕیزی grade 11ـی ڕەنگی جیاوازە؛ لە تەنیشتییەوە کارتی shilan: Student بە name "Shilan" و grade 12، بە ناونیشانی نەگۆڕاوە. لە خوارەوە: vars(aram) دەداتە {'name': 'Aram', 'grade': 11} و vars(shilan) دەداتە {'name': 'Shilan', 'grade': 12}.
Changing an attribute of one object leaves the other objects alone.گۆڕینی یەک تر ناگۆڕێت.CêIT

Attributes: each object's own dataخەسڵەتەکان: دراوەی تایبەتی هەر ئۆبجێکتێک

A typo and a missing attributeهەڵەیەکی نووسین و خەسڵەتێکی ون

Assigning to an attribute creates it if it does not exist. So a typo does not stop the program: it quietly makes a second attribute. Reading an attribute that was never made stops the program with an AttributeError:

دانانی بۆ ئەگەر بوونی نەبێت دروستی دەکات. بۆیە هەڵەیەکی نووسین ناوەستێنێت: بە بێدەنگی دووەم دروست دەکات. خوێندنەوەی کە هەرگیز دروست نەکراوە بە AttributeError دەوەستێنێت:

grdae instead of grade, and a mark that was never setgrdae لەبری grade و نمرەیەک کە هەرگیز دانەنراوەattribute_typo.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

aram = Student("Aram", 10)
aram.grdae = 11                  # typo: Python makes a NEW attribute
print(aram.grade)
print(vars(aram))
print(aram.mark)                 # this attribute was never made

aram.grdae = 11 made a new attribute grdae, and aram.grade is still 10: a silent bug, but vars() shows it. aram.mark was never assigned, so Python raises AttributeError: 'Student' object has no attribute 'mark'. Read it as: this object has no such attribute; check the spelling, or set it in __init__.

aram.grdae = 11 نوێی grdae دروست کرد و aram.grade هێشتا ⁦10⁩ـە: بێدەنگ، بەڵام vars() پیشانی دەدات. aram.mark هەرگیز بۆ دانەنراوە، بۆیە AttributeError: 'Student' object has no attribute 'mark' دروست دەکات. بەم شێوەیە بیخوێنەوە: ئەم ئەو نییە؛ ڕێنووسەکە بپشکنە، یان لە __init__ـدا دایبنێ.

Left: aram.grdae = 11, labelled a typo, a new attribute, with an arrow into the card aram: Student, which now has three rows: name "Aram", grade 10 and, highlighted, grdae 11. Below: aram.mark, labelled never made, with a dashed arrow to the card; no attribute mark here, so a box shows AttributeError: 'Student' object has no attribute 'mark'. Bottom: aram.grade is still 10; check the spelling with vars(aram).چەپ: aram.grdae = 11، بە ناونیشانی هەڵەی نووسین: خەسڵەتێکی نوێ، بە تیرێک بۆ ناو کارتی aram: Student کە ئێستا سێ ڕیزی هەیە: name "Aram"، grade 10 و بە ڕەنگی جیاواز grdae 11. لە خوارەوە: aram.mark، بە ناونیشانی هەرگیز دروست نەکراوە، بە تیرێکی پچڕپچڕ بۆ کارتەکە؛ خەسڵەتی mark لێرە نییە، بۆیە چوارگۆشەیەک AttributeError: 'Student' object has no attribute 'mark' پیشان دەدات. خوارەوە: aram.grade هێشتا 10ـە؛ ڕێنووسەکە بە vars(aram) بپشکنە.
A misspelt assignment makes a new attribute; reading a missing attribute raises AttributeError.دانانی بە ڕێنووسی هەڵە نوێ دروست دەکات؛ خوێندنەوەی ون AttributeError دروست دەکات.CêIT

Which class? type() and isinstance()کام کلاس؟ type() و isinstance()

type(x) gives the class of an object. isinstance(x, C) asks a yes-or-no question: is x an instance of the class C? It is the better check in an if, because it also says yes for classes built from C. True is an instance of int, since the class bool is built from int (inheritance):

type(x) دەدات. isinstance(x, C) پرسیارێکی بەڵێ یان نەخێر دەکات: ئایا x Cـە؟ لە ناو ifـدا پشکنینی باشترە، چونکە بۆ ئەو لە C دروست کراون بەڵێش دەڵێت. True intـە، چونکە bool لە int دروست کراوە (بۆماوە):

Students and a teacher in one listقوتابییەکان و مامۆستایەک لە یەک لیستداtype_isinstance.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Student:
    def __init__(self, name):
        self.name = name

class Teacher:
    def __init__(self, name):
        self.name = name

people = [Student("Aram"), Teacher("Hana"), Student("Karwan")]
for p in people:
    print(p.name, type(p).__name__, isinstance(p, Student))

print(isinstance(2026, int), isinstance(True, int), type(True) is int)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 11

type(p).__name__ is the class's name as text.

type(p).__name__ ناوی وەک دەق.

Use isinstance() when a function must check what it was given, for example if not isinstance(mark, int): before it uses a mark.

کاتێک دەبێت بپشکنێت چی پێ دراوە isinstance() بەکار بهێنە، بۆ نموونە if not isinstance(mark, int): پێش ئەوەی نمرەیەک بەکار بهێنێت.

Three object cards in a row: a Student object with name "Aram", a Teacher object with name "Hana" and a Student object with name "Karwan". Under each, isinstance(p, Student) gives True, False and True. Below: isinstance(True, int) gives True while type(True) is int gives False, because bool is built from int, so True is an int too.سێ کارتی ئۆبجێکت لە ڕیزێکدا: ئۆبجێکتێکی Student بە name "Aram"، ئۆبجێکتێکی Teacher بە name "Hana" و ئۆبجێکتێکی Student بە name "Karwan". لە ژێر هەریەکەیاندا isinstance(p, Student) دەداتە True، False و True. لە خوارەوە: isinstance(True, int) دەداتە True، بەڵام type(True) is int دەداتە False، چونکە bool لە int دروست کراوە، بۆیە True هەروەها intـیشە.
isinstance() answers: is this object an instance of that class?isinstance() وەڵام دەداتەوە: ئایا ئەم ئەو ؟CêIT

A variable holds a reference to an objectگۆڕاوێک ئاماژەیەک بۆ ئۆبجێکتێک هەڵدەگرێت

A variable does not contain the object itself; it holds a reference to it, like a name tag tied to a box, exactly as with lists. monitor = aram ties a second tag to the same object, so a change through one name is seen through the other. A new object with the same values is still a different object:

خودی لە خۆیدا هەڵناگرێت؛ بۆی هەڵدەگرێت، وەک تاگێکی ناو کە بە سندووقێکەوە بەسترابێت، ڕێک وەک لیستەکان. monitor = aram تاگێکی دووەم بە هەمان دەبەستێت، بۆیە گۆڕانێک لە ڕێگەی ناوێکەوە لە ڕێگەی ئەوی تریشەوە دەبینرێت. نوێ بە هەمان هێشتا ترە:

Two names, one object; a twin; a real copyدوو ناو، یەک ئۆبجێکت؛ جمکێک؛ کۆپییەکی ڕاستەقینەreferences.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
import copy

class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

aram = Student("Aram", 10)
monitor = aram                    # a second name, not a copy
monitor.grade = 11
print(aram.grade, monitor is aram)

twin = Student("Aram", 11)        # a new object with the same values
print(twin is aram, twin == aram)

spare = copy.copy(aram)           # a real copy: a new object
spare.grade = 12
print(aram.grade, spare.grade)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 9

monitor is a second name for Aram's object, not a new student.

monitor ناوێکی دووەمە بۆ ئارام، نەک قوتابییەکی نوێ.

Lineدێڕی 16

copy.copy() makes a new object with the same attributes.

copy.copy() نوێ بە هەمان دروست دەکات.

is asks "the same object?". For your own classes, == asks the same question until you teach it to compare values with __eq__ (comparing objects); that is why twin == aram is False. For objects that hold lists, also read about shallow and deep copies.

is دەپرسێت «هەمان ؟». بۆ خۆت، == هەمان پرسیار دەکات تا ئەو کاتەی بە __eq__ فێری دەکەیت بەراورد بکات (بەراوردکردنی ئۆبجێکتەکان)؛ بۆیە twin == aram Falseـە. بۆ ئەو هەڵدەگرن، هەروەها کۆپیی ڕووکەش و کۆپیی قووڵ بخوێنەوە.

The name tags aram and monitor both point to one Student object with name "Aram" and grade 11. A third tag, twin, points to a second Student object with the same values. Beside them: monitor is aram gives True; twin is aram gives False; twin == aram gives False.تاگی ناوی aram و monitor هەردووکیان ئاماژە بۆ یەک ئۆبجێکتی Student دەکەن بە name "Aram" و grade 11. تاگێکی سێیەم، twin، ئاماژە بۆ ئۆبجێکتێکی دووەمی Student دەکات بە هەمان نرخەکان. لە تەنیشتیانەوە: monitor is aram دەداتە True؛ twin is aram دەداتە False؛ twin == aram دەداتە False.
Two names can share one object; the same values in a new object make another object.دوو ناو دەتوانن یەک بەش بکەن؛ هەمان لە نوێدا تر دروست دەکەن.CêIT

A variable holds a reference to an objectگۆڕاوێک ئاماژەیەک بۆ ئۆبجێکتێک هەڵدەگرێت

Passing an object to a functionناردنی ئۆبجێکتێک بۆ کردارێک

A parameter is one more name for the object the caller gave. So a function can change an object, and the caller sees the change after the call, exactly like a list passed to a function:

ناوێکی ترە بۆ ئەو داویەتی. بۆیە دەتوانێت بگۆڕێت و دوای بانگکردنەکە گۆڕانەکە دەبینێت، ڕێک وەک لیستێک کە دەنێردرێت بۆ کردارێک:

A function that promotes a studentکردارێک کە قوتابییەک بەرز دەکاتەوەobject_into_function.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

def promote(student):
    student.grade += 1           # changes the object the caller gave

aram = Student("Aram", 10)
promote(aram)
promote(aram)
print(aram.name, aram.grade)

Inside promote(), student and the caller's aram are two tags on one object, so student.grade += 1 changes Aram's grade; called twice, it goes from 10 to 12. Such a function works on students, and a little later you move it into the class as a method.

لە ناو promote()ـدا، student و aramـی دوو تاگن لەسەر یەک ، بۆیە student.grade += 1 پۆلی ئارام دەگۆڕێت؛ دوو جار بانگ کرا، لە ⁦10⁩ـەوە دەچێتە ⁦12⁩. ئەم جۆرە لەسەر قوتابییەکان کار دەکات و کەمێک دواتر دەیگوازیتەوە ناو وەک .

Two dashed frames. Main program: the tag aram and the call promote(aram). Inside promote(student): the tag student and grade += 1. Both tags point to one Student object below, with name "Aram" and grade 10 → 12.دوو چوارچێوەی پچڕپچڕ. بەرنامەی سەرەکی: تاگی aram و بانگکردنی promote(aram). لە ناو promote(student): تاگی student و grade += 1. هەردوو تاگەکە ئاماژە بۆ یەک ئۆبجێکتی Student لە خوارەوە دەکەن، بە name "Aram" و grade 10 ← 12.
The parameter student and the caller's aram point to the same object. student و aramـی ئاماژە بۆ هەمان دەکەن.CêIT

A variable holds a reference to an objectگۆڕاوێک ئاماژەیەک بۆ ئۆبجێکتێک هەڵدەگرێت

A list of objectsلیستێک لە ئۆبجێکتەکان

A list can hold objects like any other value; each item is a reference to one object. A loop then visits every student and reads its attributes with the dot. This program prints a small table and finds the best mark:

دەتوانێت وەک هەر تر هەڵبگرێت؛ هەر دانەیەک بۆ یەک . پاشان سەردانی هەموو قوتابییەک دەکات و بە خاڵ دەخوێنێتەوە. ئەم خشتەیەکی بچووک چاپ دەکات و باشترین نمرە دەدۆزێتەوە:

Three students, one best markسێ قوتابی، یەک باشترین نمرەlist_of_objects.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Student:
    def __init__(self, name, mark):
        self.name = name
        self.mark = mark

students = [Student("Aram", 78), Student("Shilan", 93), Student("Karwan", 85)]

best = students[0]
for s in students:
    print(f"{s.name:<8}{s.mark}")
    if s.mark > best.mark:
        best = s
print("Best:", best.name)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 10

{s.name:<8} pads the name to 8 characters: format specifiers.

{s.name:<8} ناوەکە بە بۆشایی دەگەیەنێتە 8 : دیاریکەرەکانی شێواز.

Lineدێڕی 12

best refers to the best object so far; at the end it is Shilan's.

best ئاماژە بۆ باشترین تا ئێستا دەکات؛ لە کۆتاییدا هی شیلانە.

best = s copies a reference, not a student: best simply points at the object with the top mark. With objects, the name and the mark of each student stay together, so they can never get out of step as two parallel lists can.

best = s کۆپی دەکات، نەک قوتابییەک: best تەنیا ئاماژە بۆ ئەو دەکات کە بەرزترین نمرەی هەیە. بە ، ناو و نمرەی هەر قوتابییەک پێکەوە دەمێننەوە، بۆیە هەرگیز لە یەک جیا نابنەوە، بە پێچەوانەی دوو تەریب.

The list students with three slots [0], [1] and [2]; an arrow leads from each slot to a Student object: Aram 78, Shilan 93 and Karwan 85. The tag best points to Shilan's object. Bottom: best = s copies a reference; best points at the object with the top mark.لیستی students بە سێ خانە [0]، [1] و [2]؛ لە هەر خانەیەکەوە تیرێک دەچێت بۆ ئۆبجێکتێکی Student: Aram 78، Shilan 93 و Karwan 85. تاگی best ئاماژە بۆ ئۆبجێکتی شیلان دەکات. خوارەوە: best = s ئاماژەیەک کۆپی دەکات؛ best ئاماژە بۆ ئەو ئۆبجێکتە دەکات کە بەرزترین نمرەی هەیە.
Each item of the list is a reference to one student object.هەر دانەیەکی بۆ یەک قوتابی.CêIT

A first method: what an object can doیەکەم میتۆد: ئۆبجێکتێک دەتوانێت چی بکات

A method is a function written inside a class. Its first parameter is self, so it can use the object's attributes. A method can return a value made from the attributes, or change them:

کە لە ناو نووسراوە. یەکەم selfـە، بۆیە دەتوانێت بەکار بهێنێت. دەتوانێت کە لە دروست کراوە بگەڕێنێتەوە، یان ئەوان بگۆڕێت:

describe() returns, promote() changes⁦describe()⁩ دەگەڕێنێتەوە، ⁦promote()⁩ دەگۆڕێتfirst_method.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

    def describe(self):              # a method that returns text
        return f"{self.name} is in grade {self.grade}"

    def promote(self):               # a method that changes the object
        self.grade += 1

aram = Student("Aram", 10)
print(aram.describe())
aram.promote()
print(aram.describe())

aram.promote() changes Aram only, because self is Aram. Compare it with promote(student) two screens back: the code is the same, but now it lives with the data it changes, and every Student object has it. Methods with parameters, methods that return answers and Python's special methods are in the next lesson, Attributes and methods.

aram.promote() تەنیا ئارام دەگۆڕێت، چونکە self ئارامە. بەراوردی بکە لەگەڵ promote(student) دوو شاشە پێشتر: کۆدەکە هەمانە، بەڵام ئێستا لەگەڵ ئەو دەژی کە دەیگۆڕێت و هەموو Student هەیەتی. بە ، ئەو وەڵام دەگەڕێننەوە و لە وانەی داهاتوودان: خەسڵەتەکان و میتۆدەکان.

Before: the card aram: Student with name "Aram" and grade 10. An arrow leads through the box aram.promote(), which runs self.grade += 1, to after: the same card with grade 11 highlighted. Below: self is aram, so only Aram moves up a grade; aram.describe() now gives "Aram is in grade 11".پێش: کارتی aram: Student بە name "Aram" و grade 10. تیرێک بە ناو چوارگۆشەی aram.promote()دا تێدەپەڕێت کە self.grade += 1 جێبەجێ دەکات، بۆ دوای: هەمان کارت بە grade 11 بە ڕەنگی جیاواز. لە خوارەوە: self هەمان aramـە، بۆیە تەنیا ئارام پۆلێک دەچێتە سەرەوە؛ aram.describe() ئێستا دەداتە "Aram is in grade 11".
A method changes the object it is called on. ئەو دەگۆڕێت کە لەسەری بانگ دەکرێت.CêIT

Printing an objectچاپکردنی ئۆبجێکتێک

print() of your own object shows only its class and memory address, which tells a reader nothing:

print()ـی خۆت تەنیا و پیشان دەدات کە هیچ شتێک بە خوێنەر نالێت:

The default text of an objectدەقی بنەڕەتیی ئۆبجێکتێکprinting_default.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

aram = Student("Aram", 10)
print(aram)

Add a method named __str__ that returns the text you want. print() and str() call it for you:

بە ناوی __str__ زیاد بکە کە ئەو دەقەی دەتەوێت بگەڕێنێتەوە. print() و str() خۆیان بانگی دەکەن:

A __str__ methodمیتۆدی __str__printing_str.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

    def __str__(self):               # the text print() should show
        return f"{self.name} (grade {self.grade})"

aram = Student("Aram", 10)
print(aram)
print("Top student: " + str(aram))

__str__ is one of Python's special methods: names with two underscores on each side, like __init__, that Python calls by itself. The next lesson shows __str__ beside __repr__ and __eq__: __str__ and __repr__. Java does the same with toString(): printing objects in Java.

__str__ یەکێکە لە : ئەو ناوانەی دوو هێڵی ژێرەوەیان لە هەر لایەکەوە هەیە، وەک __init__، کە خۆی بانگیان دەکات. وانەی داهاتوو __str__ لە تەنیشت __repr__ و __eq__ پیشان دەدات: ⁦__str__⁩ و ⁦__repr__⁩. هەمان کار بە toString() دەکات: چاپکردنی ئۆبجێکتەکان لە Java.

Two terminal panels. Without __str__: >>> print(aram) shows <__main__.Student object at 0x109b9e900>. With __str__: >>> print(aram) shows Aram (grade 10). Bottom: __str__ returns the text that print() and str() show.دوو پانێڵی تێرمیناڵ. بێ __str__: >>> print(aram) پیشان دەدات <__main__.Student object at 0x109b9e900>. لەگەڵ __str__: >>> print(aram) پیشان دەدات Aram (grade 10). خوارەوە: __str__ ئەو دەقە دەگەڕێنێتەوە کە print() و str() پیشانی دەدەن.
The same print(aram) before and after the class gets a __str__ method.هەمان print(aram) پێش و دوای ئەوەی __str__ وەربگرێت.CêIT

Common mistake: forgetting selfهەڵەی باو: لەبیرچوونی self

The most common error with a first class is a method without self. Python always passes the object as the first argument, so a method with no parameters has no place for it:

باوترین هەڵە لە یەکەم بێ self. هەمیشە وەک یەکەم دەنێرێت، بۆیە کە نییە شوێنی بۆی نییە:

A method without selfمیتۆدێک بێ selfmistake_no_self.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Student:
    def __init__(self, name):
        self.name = name

    def describe():                  # self is missing
        return "a student"

aram = Student("Aram")
print(aram.describe())

Read the last line of the traceback: describe() takes 0 positional arguments but 1 was given. The one argument is aram, which Python sent. The fix is def describe(self):.

دوا دێڕی ڕێچکەی هەڵە بخوێنەوە: describe() ⁦takes 0 positional arguments but 1 was given⁩. ئەو یەک aramـە، کە ناردی. چارەسەرەکە def describe(self):ـە.

aram.describe() sends 1 argument, aram. An arrow from aram leads to def describe(): in an orange box, labelled no place for it, with TypeError: ... takes 0 positional. A second arrow leads to def describe(self): in a green box, labelled self receives aram, giving "Aram is in grade 10". Left: Fix: self first, in every method.aram.describe() 1 ئارگیومێنت دەنێرێت: aram. تیرێک لە aramـەوە دەچێت بۆ def describe(): لە چوارگۆشەیەکی پرتەقاڵیدا، بە ناونیشانی شوێنی بۆی نییە و TypeError: ... takes 0 positional. تیرێکی دووەم دەچێت بۆ def describe(self): لە چوارگۆشەیەکی سەوزدا، بە ناونیشانی self ئارام وەردەگرێت، کە "Aram is in grade 10" دەدات. چەپ: چارەسەر: self لە پێشەوە، لە هەموو میتۆدێکدا.
Python always passes the object: every method needs self first. هەمیشە دەنێرێت: هەموو پێویستی بە self لە پێشەوە هەیە.CêIT

Common mistake: forgetting selfهەڵەی باو: لەبیرچوونی self

More common mistakesهەڵە باوەکانی تر

Each mistake below was run with Python 3.13; the table shows the last line Python printed and the fix.

هەریەکە لەم هەڵانەی خوارەوە بە ⁦3.13⁩ تاقی کرایەوە؛ خشتەکە دوا دێڕێک کە چاپی کرد و چارەسەرەکە پیشان دەدات.

Five more beginner mistakes with classes (checked with Python 3.13).پێنج هەڵەی تری تازەکاران لەگەڵ (بە ⁦3.13⁩ پشکنراون).
MistakeهەڵەWhat Python says چی دەڵێتFixچارەسەر
name = name in __init__name = name لە __init__ـداAttributeError: 'Student' object has no attribute 'name'self.name = name: without self. it is only a local variable that disappears.self.name = name: بەبێ self. تەنیا کە ون دەبێت.
Student("Aram") when __init__ needs a name and a gradeStudent("Aram") کاتێک __init__ ناو و پۆلی دەوێتTypeError: Student.__init__() missing 1 required positional argument: 'grade'Pass every value __init__ asks for (or give it a default).هەموو ئەو __init__ داوایان دەکات بنێرە (یان بۆ دابنێ).
def _init_(self, name): with one underscoredef _init_(self, name): بە یەک هێڵی ژێرەوەTypeError: Student() takes no argumentsTwo underscores on each side: __init__.دوو هێڵی ژێرەوە لە هەر لایەک: __init__.
return a value from __init__گەڕاندنەوەی لە __init__TypeError: __init__() should return None, not 'str'__init__ only fills in the object; leave out return.__init__ تەنیا پڕ دەکاتەوە؛ return لاببە.
print(aram.describe) without bracketsprint(aram.describe) بێ کەوانە<bound method Student.describe of <__main__.Student object at 0x104232900>>No error, but the method was not called: write aram.describe().هەڵە نییە، بەڵام بانگ نەکرا: aram.describe() بنووسە.

Exercisesڕاهێنانەکان

ExerciseڕاهێنانA Teacher classکلاسی Teacher

Write a class Teacher with the attributes name and subject and a method introduce() that returns a sentence such as "I am Hana and I teach Maths.". Make two teachers and print both introductions.

Teacher بنووسە بە name و subject و introduce() کە ڕستەیەکی وەک "I am Hana and I teach Maths." بگەڕێنێتەوە. دوو مامۆستا دروست بکە و ناساندنی هەردووکیان چاپ بکە.

Hintئاماژە

Copy the shape of Student: __init__ stores the two values with self., and the method uses self.name and self.subject.

شێوەی Student کۆپی بکە: __init__ دوو بە self. هەڵدەگرێت و self.name و self.subject بەکار دەهێنێت.

Show answerوەڵامەکە پیشان بدە
One possible answerوەڵامێکی گونجاوteacher.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Teacher:
    def __init__(self, name, subject):
        self.name = name
        self.subject = subject

    def introduce(self):
        return f"I am {self.name} and I teach {self.subject}."

hana = Teacher("Hana", "Maths")
soran = Teacher("Soran", "Physics")
print(hana.introduce())
print(soran.introduce())
ExerciseڕاهێنانPredict the outputدەرخستەکە پێشبینی بکە

Without running it, write down what this program prints. Then explain each of the four values.

بەبێ کارپێکردنی، بنووسە ئەم چی چاپ دەکات. پاشان هەریەکە لە چوار ڕوون بکەرەوە.

predict.pyPYTHON
class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

a = Student("Rebin", 9)
b = a
b.grade = 10
c = Student("Rebin", 10)
print(a.grade, a is b, a is c, a == c)
Show answerوەڵامەکە پیشان بدە
The outputدەرخستەکەpredict.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

a = Student("Rebin", 9)
b = a
b.grade = 10
c = Student("Rebin", 10)
print(a.grade, a is b, a is c, a == c)

b = a makes a second name for the same object, so b.grade = 10 changes a.grade too: 10, and a is b is True. c is a new object with the same values, so a is c is False, and a == c is also False, because without __eq__ == compares identity.

b = a ناوێکی دووەم بۆ هەمان دروست دەکات، بۆیە b.grade = 10 a.gradeـیش دەگۆڕێت: ⁦10⁩ و a is b Trueـە. c نوێیە بە هەمان ، بۆیە a is c Falseـە و a == cـیش Falseـە، چونکە بەبێ __eq__، == ناسنامە بەراورد دەکات.

ExerciseڕاهێنانFind the mistakesهەڵەکان بدۆزەوە

This class has two errors and one style problem. Find all three and write the corrected class.

ئەم دوو هەڵە و کێشەیەکی شێوازی هەیە. هەر سێکیان بدۆزەوە و ڕاستکراوەکە بنووسە.

buggy_course.pyPYTHON
class course:
    def __init__(title, hours):
        self.title = title
        hours = hours

    def describe(self):
        return f"{self.title}: {self.hours} hours"

c = course("Python 1", 4)
print(c.describe())
Show answerوەڵامەکە پیشان بدە

(1) __init__ has no self, so Python stops at once: TypeError: course.__init__() takes 2 positional arguments but 3 were given. (2) hours = hours stores nothing in the object; after fixing (1), describe() stops with AttributeError: 'course' object has no attribute 'hours'. (3) Style: class names are written in CapWords, Course. The corrected program:

⁦(1)⁩ __init__ selfـی نییە، بۆیە یەکسەر دەوەستێت: TypeError: course.__init__() takes 2 positional arguments but 3 were given. ⁦(2)⁩ hours = hours هیچ شتێک لە هەڵناگرێت؛ دوای چاککردنی ⁦(1)⁩، describe() بە AttributeError: 'course' object has no attribute 'hours' دەوەستێت. ⁦(3)⁩ شێواز: ناوی بە CapWords دەنووسرێن، Course. ڕاستکراوەکە:

The corrected classکلاسە ڕاستکراوەکەfixed_course.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Course:
    def __init__(self, title, hours):
        self.title = title
        self.hours = hours

    def describe(self):
        return f"{self.title}: {self.hours} hours"

c = Course("Python 1", 4)
print(c.describe())
ExerciseڕاهێنانCourses with enough hoursئەو کۆرسانەی کاتژمێری پێویستیان هەیە

Make a list of four Course objects with a title and the hours per week: Python 1 (4), Maths (3), English (2), Physics (3). Print every course with at least 3 hours, then the total hours of all courses.

لە چوار Course دروست بکە بە title و hoursـی هەفتانە: ⁦Python 1 (4)⁩، ⁦Maths (3)⁩، ⁦English (2)⁩، ⁦Physics (3)⁩. هەموو کۆرسێک کە لانیکەم 3 کاتژمێری هەیە چاپ بکە، پاشان کۆی کاتژمێرەکانی هەموو کۆرسەکان.

Show answerوەڵامەکە پیشان بدە
One possible answerوەڵامێکی گونجاوcourses_hours.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Course:
    def __init__(self, title, hours):
        self.title = title
        self.hours = hours          # hours per week

courses = [Course("Python 1", 4), Course("Maths", 3), Course("English", 2), Course("Physics", 3)]

total = 0
for c in courses:
    total += c.hours
    if c.hours >= 3:
        print(c.title, c.hours)
print("Total hours:", total)
ExerciseڕاهێنانA library bookکتێبێکی کتێبخانە

The school library needs a class Book. A new book has a title and is available. borrow() returns "You borrowed …" and marks the book as not available, or returns "… is already borrowed". give_back() makes it available again. Test it by borrowing the same book twice, giving it back and borrowing it again.

کتێبخانەی قوتابخانە پێویستی بە Book هەیە. کتێبێکی نوێ titleـی هەیە و بەردەستە. borrow() "You borrowed …" دەگەڕێنێتەوە و کتێبەکە وەک نابەردەست نیشان دەکات، یان "… is already borrowed" دەگەڕێنێتەوە. give_back() دووپاتە بەردەستی دەکاتەوە. تاقی بکەرەوە: هەمان کتێب دوو جار بخوازە، بیگەڕێنەوە و دووپاتە بیخوازە.

Show answerوەڵامەکە پیشان بدە
One possible answerوەڵامێکی گونجاوbook.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
class Book:
    def __init__(self, title):
        self.title = title
        self.available = True        # every new book is on the shelf

    def borrow(self):
        if not self.available:
            return f"{self.title} is already borrowed"
        self.available = False
        return f"You borrowed {self.title}"

    def give_back(self):
        self.available = True

book = Book("Kurdish Poems")
print(book.borrow())
print(book.borrow())
book.give_back()
print(book.borrow())

available is an instance attribute that the methods read and change: the object remembers its own state between calls.

available کە دەیخوێننەوە و دەیگۆڕن: لە نێوان بانگکردنەکاندا دۆخی خۆی لە بیر دەگرێت.

Summaryپوختە

  • Procedural programming keeps data and functions apart; object-oriented programming bundles each piece of data with its actions in an object. Its four ideas: encapsulation, abstraction, inheritance, polymorphism.
  • A class is the plan (class Student:, CapWords); calling it, Student(...), makes an object, an instance of the class. Every Python value is an instance of some class.
  • __init__(self, ...) fills in each new object; self.name = name makes an instance attribute. self is the object the method was called on: aram.describe() is Student.describe(aram).
  • Read and change attributes with a dot; a misspelt assignment makes a new attribute, and reading a missing one raises AttributeError. vars() lists them.
  • type(x) gives the class; isinstance(x, C) checks it and also accepts classes built from C.
  • Variables, parameters and list items hold references: b = a shares one object; is checks identity; copy.copy() makes a new object.
  • A method is a function in the class with self first; it returns a value or changes the object. __str__ returns the text print() shows.
  • و جیا دەهێڵێتەوە؛ هەر پارچە لەگەڵ کارەکانی لە کۆ دەکاتەوە. چوار بیرۆکەکەی: ، ، ، .
  • نەخشەکەیە (class Student:، CapWords)؛ بانگکردنی، Student(...)، دروست دەکات کە . هەموو .
  • __init__(self, ...) هەر نوێ پڕ دەکاتەوە؛ self.name = name دروست دەکات. self ئەو کە لەسەری بانگ کراوە: aram.describe() هەمان Student.describe(aram)ـە.
  • بە خاڵ بخوێنەوە و بیانگۆڕە؛ دانانی بە ڕێنووسی هەڵە نوێ دروست دەکات و خوێندنەوەی ون AttributeError دروست دەکات. vars() لیستیان دەکات.
  • type(x) دەدات؛ isinstance(x, C) دەیپشکنێت و ئەو قبووڵ دەکات کە لە C دروست کراون.
  • ، و دانەکانی هەڵدەگرن: b = a یەک بەش دەکات؛ is ناسنامە دەپشکنێت؛ copy.copy() نوێ دروست دەکات.
  • لە ناو بە self لە پێشەوە؛ دەگەڕێنێتەوە یان دەگۆڕێت. __str__ ئەو دەقە دەگەڕێنێتەوە کە print() پیشانی دەدات.

Next: Attributes and methods: class attributes, more kinds of methods and the special methods __repr__ and __eq__.

داهاتوو: خەسڵەتەکان و میتۆدەکان: ، جۆرەکانی تری و __repr__ و __eq__.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Attributes and Methodsخەسڵەتەکان و میتۆدەکان

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.

Appearanceڕووکار

Modeدۆخ
Styleشێواز
Coloursڕەنگەکان

Saved on this device only.تەنیا لەسەر ئەم ئامێرە پاشەکەوت دەکرێت.