Hướng dẫn __contains__ method python
Here is my code:
Chris Martin 29.6k8 gold badges74 silver badges131 bronze badges asked Dec 27, 2009 at 1:49 Like all special methods (with "magic names" that begin and end in With your class answered Dec 27, 2009 at 2:04 Alex MartelliAlex Martelli 821k163 gold badges1200 silver badges1377 bronze badges to get your code to do something (although nothing useful):
The docs. Julius 7997 silver badges11 bronze badges answered Dec 27, 2009 at 1:51 cobbalcobbal 68.9k19 gold badges142 silver badges155 bronze badges
Azat Ibrakov 8,8669 gold badges37 silver badges45 bronze badges answered Jan 1, 2013 at 9:33 vijay shankervijay shanker 2,3781 gold badge21 silver badges37 bronze badges 1
I think what you probably meant is:
answered Dec 27, 2009 at 1:56 bobincebobince 518k102 gold badges644 silver badges825 bronze badges Lets see a very simple example of magic method __contains__ : Suppose I have class Player and my __init__ method takes one string argument name. In main I have created an object (obj1) of class Player. Now if I want to know if my obj1 (in this case attribute name of obj1) contains a particular string, substring or an alphabet, I have to implement __contains__ method as shown in the example. If my class has __contains__ method I can call built-in operator in on my custom objects as shown in the example.
answered Jul 29, 2016 at 10:00 N RandhawaN Randhawa 7,9833 gold badges41 silver badges47 bronze badges Not the answer you're looking for? Browse other questions tagged python or ask your own question. |