தீர்க்கப்பட்டது: பைதான் __sub__ மேஜிக் முறை

பைத்தானில் உள்ள __sub__ மேஜிக் முறை இரண்டு வாதங்களை எடுக்கும் ஒரு செயல்பாட்டை அழைக்கப் பயன்படுகிறது, ஆனால் முதல் வாதம் இரண்டாவது வாதத்தின் வகுப்பின் துணைப்பிரிவாக விளக்கப்படுகிறது. துணைப்பிரிவு __sub__ மேஜிக் முறையைச் செயல்படுத்தவில்லை என்றால், இது எதிர்பாராத நடத்தைக்கு வழிவகுக்கும்.

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)

மேஜிக் முறை என்றால் என்ன

பைத்தானில், மேஜிக் முறை என்பது ஒரு சிறப்பு வகை செயல்பாடு ஆகும், இது ஒரு செயல்பாட்டை அதன் பெயரைக் குறிப்பிடாமல் அழைக்க உங்களை அனுமதிக்கிறது. செயல்பாட்டின் பெயரை ஒரு ஆம்பர்சண்ட் (&) உடன் முன்னொட்டு வைப்பதன் மூலம் இது செய்யப்படுகிறது.

மேஜிக் முறைகளின் பட்டியல்

பைத்தானில் மேஜிக் செய்ய பல்வேறு வழிகள் உள்ளன. இதோ சில:

1. சீரற்ற இறக்குமதி
2. இறக்குமதி நேரம்
3. கணித இறக்குமதி சதுரத்திலிருந்து, பை
4. தேதியிலிருந்து இறக்குமதி தேதி, நேரம்
5. ஆபரேட்டர் இறக்குமதியிலிருந்து சேர், கழித்தல், பெருக்கி, வகுத்தல்
6. ஃபங்க்டூல்களில் இருந்து பகுதி இறக்குமதி
7. சேகரிப்புகள் இறக்குமதி deque இருந்து

தொடர்புடைய இடுகைகள்:

ஒரு கருத்துரையை