Indlela __sub__ yomlingo kwiPython isetyenziselwa ukubiza umsebenzi othatha iimpikiswano ezimbini, kodwa ingxoxo yokuqala itolikwa njengodidi olusezantsi lwengxoxo yesibini. Oku kunokukhokelela ekuziphatheni okungalindelekanga ukuba udidi olusezantsi aluphumezi __sub__ indlela yomlingo.
The __sub__ magic method is used to implement the subtraction operator for objects. It is called when the - operator is used on two objects of the same type. The __sub__ method should return the result of the subtraction operation. For example, if we have a class that represents a complex number, we could define the __sub__ method like this: class ComplexNumber: def __init__(self, real, imaginary): self.real = real self.imaginary = imaginary def __sub__(self, other): return ComplexNumber(self.real - other.real, self.imaginary - other.imaginary) Now we can use the - operator on two ComplexNumber objects: c1 = ComplexNumber(1, 2) c2 = ComplexNumber(3, 4) c3 = c1 - c2 print(c3) # Prints ComplexNumber(real=-2, imaginary=-2)
Yintoni Indlela yomlingo
KwiPython, indlela yomlingo luhlobo olukhethekileyo lomsebenzi okuvumela ukuba ubize umsebenzi ngaphandle kokuchaza igama lawo. Oku kwenziwa ngokumisela igama lomsebenzisi ngeampersand (&).
Uluhlu lweendlela zomlingo
Zininzi iindlela ezahlukeneyo zokwenza umlingo kwiPython. Nazi ezimbalwa:
1. ukungenisa random
2. ixesha lokungenisa
3. ukusuka kwimathematika yokungenisa sqrt, pi
4. ukusuka kumhla wokungenisa ngexesha, ixesha
5. ukusuka kumsebenzisi ongenisayo yongeza, thabatha, phindaphinda, yahlula
6. ukusuka kwi-functools yokungenisa inxalenye
7. ukusuka kwingqokelela ukungenisa deque