1 / 6

Python – OOP

Python – OOP. Ulomki, seštevanje, množenje. + nad objekti neznanega razreda. Uvodno predavanje – tam smo omenili: class Ulomek : def __ init __( self , st, im) : self.stevec = st self.imenovalec = im def __ add __( self , ulP ) : novŠtevec = self.stevec * ul.imenovalec +

lilli
Download Presentation

Python – OOP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python – OOP Ulomki, seštevanje, množenje

  2. + nad objekti neznanega razreda Uvodno predavanje – tam smo omenili: classUlomek : def __init__(self, st, im) : self.stevec = st self.imenovalec = im def __add__(self, ulP) : novŠtevec = self.stevec * ul.imenovalec + ulP.stevec * self.imenovalec novImenovalec = self.imenovalec * ulP.imenovalec return Ulomek(novŠtevec, novImenovalec) def izpis(self) : print(str(self.stevec) + " / " + str(self.imenovalec), end='') mojUl = Ulomek(3, 5) polovica = Ulomek(1, 2) mojUl = mojUl + polovica

  3. Včasih bi radi uporabljali • *, -, ==, >, >= … • Vse to lahko počnemo tudi z objekti novih razredov • Le ustrezno poimenovano metodo moramo napisati • Vse možnosti: • http://docs.python.org/dev/3.0/reference/datamodel.html#special-method-names

  4. Primerjave • < • def__lt__(self, drugObjekt) • <= • def__le__(self, drugObjekt) • == • def__eq__(self, drugObjekt) • != • def__ne__(self, drugObjekt) • > • def__gt__(self, drugObjekt) • >= • def__ge__(self, drugObjekt) • Vse metode naj vrnejo True/False

  5. Aritmetika • + • def __add__(self, drugObjekt) • - • def __sub__(self, drugObjekt) • * • def __mul__(self, drugObjekt) • / • def __truediv__(self, drugObjekt) • // • def __floordiv__(self, drugObjekt) • % • def __mod__(self, drugObjekt) • Vse metode vrnejo nov objekt te vrste • Če torej računamo x + y, je torej isto, kot če bi napisali x.__add__(y)

  6. In že znano • def __init__ • def __str__ • def __repr__

More Related