निराकरण: Python __sub__ जादूची पद्धत

Python मधील __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)

जादूची पद्धत काय आहे

Python मध्ये, जादूची पद्धत एक विशेष प्रकारची फंक्शन आहे जी तुम्हाला फंक्शनचे नाव न सांगता कॉल करण्याची परवानगी देते. हे फंक्शनच्या नावाला अँपरसँड (&) सह उपसर्ग लावून केले जाते.

जादूच्या पद्धतींची यादी

Python मध्ये जादू करण्याचे बरेच वेगवेगळे मार्ग आहेत. येथे काही आहेत:

1. यादृच्छिक आयात करा
2. आयात वेळ
3. गणित आयात sqrt, pi
4. तारखेपासून आयात तारीख, वेळ
5. ऑपरेटर आयात वरून जोडा, वजा करा, गुणाकार करा, भागा
6. फंक्‍टूलमधून आंशिक आयात करा
7. कलेक्शन इंपोर्ट डेकमधून

संबंधित पोस्ट:

एक टिप्पणी द्या