ಪರಿಹರಿಸಲಾಗಿದೆ: ಪೈಥಾನ್ __ಉಪ__ ಮ್ಯಾಜಿಕ್ ವಿಧಾನ

ಪೈಥಾನ್‌ನಲ್ಲಿನ __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. ಗಣಿತ ಆಮದು sqrt, pi ನಿಂದ
4. ದಿನಾಂಕದಿಂದ ಆಮದು ದಿನಾಂಕ, ಸಮಯ
5. ಆಪರೇಟರ್ ಆಮದು ಸೇರಿಸಿ, ಕಳೆಯಿರಿ, ಗುಣಿಸಿ, ಭಾಗಿಸಿ
6. ಫಂಕ್ಟೂಲ್‌ಗಳಿಂದ ಆಮದು ಭಾಗಶಃ
7. ಸಂಗ್ರಹಣೆಗಳಿಂದ ಆಮದು ಡೀಕ್ಯೂ

ಸಂಬಂಧಿತ ಪೋಸ್ಟ್ಗಳು:

ಒಂದು ಕಮೆಂಟನ್ನು ಬಿಡಿ