Classes
A class describes a kind of object: which data it holds and what can be done with it. Each object made from the class has its own data.
Defining a class
A class is defined with sinf. Its body consists of methods, which are functions that belong to the class. The method named yarat is special: it runs when an object is created and usually sets up the object's fields.
sinf Hisob:
funksiya yarat(egasi, qoldiq=0):
bu.egasi = egasi
bu.qoldiq = qoldiq
funksiya kirim(summa):
bu.qoldiq += summa
funksiya chiqim(summa):
agar summa > bu.qoldiq:
tashla "Mablag' yetarli emas"
bu.qoldiq -= summa
h = Hisob("Ali", 1000)
h.kirim(500)
h.chiqim(200)
chiqar(h.egasi, h.qoldiq)
h.valyuta = "so'm"
chiqar(h.valyuta, tur(h), turdanmi(h, Hisob))Ali 1300 so'm Hisob rost
- An object is created by calling the class like a function:
Hisob("Ali", 1000). The arguments go toyarat. - Inside a method,
buis the object the method was called on. It is always available and is not written in the parameter list. - A field is created by assigning to it:
bu.qoldiq = qoldiq. Fields can also be added from outside, ash.valyutashows. tur(h)gives the name of the object's class, andturdanmi(h, Hisob)tests whether an object belongs to a class.
A class body contains methods only. Shared values are kept in module variables or returned by static methods.
Inheritance
A class can be built on another one by naming it in brackets. The child class has all the methods of its parent, may add new ones, and may replace existing ones. Inside a method, ota calls the parent's version of a method.
sinf Hayvon:
funksiya yarat(ism, ovoz):
bu.ism = ism
bu.ovoz = ovoz
funksiya gapir():
chiqar(f"{bu.ism}: {bu.ovoz}!")
sinf Mushuk(Hayvon):
funksiya yarat(ism):
ota.yarat(ism, "Miyov")
har hayvon ichida [Hayvon("Olapar", "Vov"), Mushuk("Momiq")]:
hayvon.gapir()Olapar: Vov! Momiq: Miyov!
A longer example with three levels. Shakl.tavsif calls bu.yuza(), and each object answers with the yuza of its own class:
sinf Shakl:
funksiya yarat(nom):
bu.nom = nom
funksiya yuza():
qaytar 0
funksiya tavsif():
qaytar f"{bu.nom}, yuzasi {bu.yuza()}"
sinf Tortburchak(Shakl):
funksiya yarat(eni, boyi):
ota.yarat("To'rtburchak")
bu.eni = eni
bu.boyi = boyi
funksiya yuza():
qaytar bu.eni * bu.boyi
sinf Kvadrat(Tortburchak):
funksiya yarat(tomon):
ota.yarat(tomon, tomon)
bu.nom = "Kvadrat"
har shakl ichida [Shakl("Nuqta"), Tortburchak(3, 4), Kvadrat(5)]:
chiqar(shakl.tavsif())
chiqar(turdanmi(Kvadrat(1), Shakl), avlodmi(Kvadrat, Tortburchak))Nuqta, yuzasi 0 To'rtburchak, yuzasi 12 Kvadrat, yuzasi 25 rost rost
turdanmi(x, Sinf) is also true for objects of child classes, and avlodmi(A, B) tests whether class A is B itself or is derived from it.
Computed fields and static methods
xossa funksiya defines a field that is computed each time it is read. It is used without brackets, like an ordinary field, and cannot be assigned to.
statik funksiya defines a method that belongs to the class and not to an object. It has no bu and is called on the class itself. A typical use is a second way of creating objects.
sinf Doira:
funksiya yarat(r):
bu.r = r
xossa funksiya yuza():
qaytar 3.14159 * bu.r ** 2
statik funksiya birlik():
qaytar Doira(1)
d = Doira(2)
chiqar(d.yuza)
chiqar(Doira.birlik().yuza)12.56636 3.14159
The words xossa and statik are special only directly before funksiya inside a class; elsewhere they are ordinary names.
Working with fields by name
Sometimes the name of a field is known only while the program runs. These built-in functions read, set and list fields by name:
sinf Nuqta:
funksiya yarat(x, y):
bu.x = x
bu.y = y
n = Nuqta(3, 4)
chiqar(xossa_bormi(n, "x"), xossa_ol(n, "z", 0))
xossa_qo'y(n, "z", 5)
chiqar(xossalar(n))rost 0 ["x", "y", "yarat", "z"]
| Function | Meaning |
|---|---|
xossa_bormi(o, nom) |
does the object have this field or method |
xossa_ol(o, nom[, standart]) |
read it; without a default, a missing name is an error |
xossa_qo'y(o, nom, qiymat) |
set a field |
xossa_o'chir(o, nom) |
remove a field |
xossalar(o) |
sorted list of the names of fields and methods |
Good to know
- A method may have default values and be called with named arguments, like any function.
- A method may be named like a keyword, for example
ber, because methods are always reached through a dot. - A method taken from an object, such as
f = h.kirim, remembers its object and can be called later asf(100). - Objects are compared by identity: two different objects are not equal even when their fields are.
- Version 0.1 has no operator overloading and no custom text form for objects.
Next
Errors explains what happens when something goes wrong and how a program can deal with it.