1 classRectangle (): # There are no brackets.2 def __init__ (self,x,y):3self.x=x4self.y=y5 6 def getperi (self):7 return(SELF.X+SELF.Y) *28 9 def getarea (self):Ten returnself.x*Self.y One ARect=rectangle (3,4) - -Print'The circumference of this rectangle is'+Str (rect.getperi ())) the -Print'The area of this rectangle is'+Str (Rect.getarea ())) - - classA: + def __init__ (self): - return 'A of A-cup' + AA=A () at -# __init__ shouldreturnNONE not'Str'
1=============== restart:c:/users/administrator/desktop/New. PY ===============2 The circumference of this rectangle is3 The area of this rectangle is4 Traceback (most recent):5File"c:/users/administrator/desktop/new.py", line A,inch<module>6A=A ()7TypeError: __init__ () shouldreturnNone, not'Str'8>>>
The awkward thing is that you can't understand what's underneath .... (more embarrassing, I looked at it again.)
1 classcapstr (str):2Def __new__ (CLS,string): # The first method to be called when an object is instantiated. 3 string=string. Upper () # Her first argument is that the CLS is the class. #Newneed to return an instance object4 returnSTR.__NEW__ (CLS,string)5#returnSuper (). __new__ (CLS,string# let STR begin processing after the incoming string becomes uppercase. And return this is a very clever way to handle it.6 7A=CAPSTR ('I Love fishc.com')8 print (a)9 # The new is rewritten and the string is processed before invoking the original STR's newTen# When overriding a method in a subclass, you want the method of the parent class to not disappear, then use the method of line fourth or line fifth.
.
1 =============== restart:c:/users/administrator/desktop/newpy ===============2I Love fishc.com! 3
Supplement.
1str1='Jack'2 Print (ID (str1))3str1='JACK'4 Print (ID (str1))5Print'------------is not an address-----is obviously not the same variable.---------')6num=37 print (num)8num+=59Print (num)
1 =============== restart:c:/users/administrator/desktop/newpy ===============2 484049923484051524 ------------not an address-----is obviously not the same variable.---------5 1522030896615220309767
Destructors .
1>>>classC:2 def __init__(self):3 Print('I am the __init__ method, I was called ...')4 def __del__(self):5 Print('I am the __del__ method, I was called ...')6 7>>> a=8 syntaxerror:invalid Syntax9>>>Ten>>> One>>> a=C () A I am the __init__ method, I was called ... ->>> b=a ->>>defa the syntaxerror:invalid Syntax ->>>dela ->>>delb -I am the __del__ method, I was called ...
--------------------------------------------------------------------------------------------------------------- -------------------
classNew_int (int):#defines a new class that inherits the Int class def __add__(Self,other):#rewrite + operator # __add__ is the behavior of + in int returnInt.__sub__(Self,other)#The overridden addition operator calls the subtraction operator inside the Int class def __sub__(self,other):returnInt.__add__(Self,other)#the above is a little prank. The names of addition and subtraction are exchanged.
1=============== restart:c:/users/administrator/desktop/new.py ===============2>>> 5+63114>>> a=55>>> b=66>>> A +b7118>>> C=new_int (7)9>>> D=new_int (10)Ten>>> c+D One-3 A>>> a=New_int (a) ->>> b=New_int (b) ->>> A +b the-1 ->>>#the A=new_int () was used because it did not initialize the equals operator of the int type.
--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------
1 #The following procedure is used to see how you think. See if you can figure out the problem.2 3 4 classtry_int (int):5 def __add__(self,other):6 returnself+ Other7 8 def __sub__(self,other):9 returnself+ OtherTen One #this overloads the addition and subtraction of the int class. But there's a problem with overloading. There is no right meaning for addition . A #here it is just that the + method calls the addition. This creates infinite recursion/
1=============== restart:c:/users/administrator/desktop/new.py ===============2>>> a=53>>> b=64>>> A +b5116>>> 5+67118>>> A=try_int (5)9>>> B=try_int (6)Ten>>> A +b One Traceback (most recent): AFile"<pyshell#6>", Line 1,inch<module> -A +b -File"c:/users/administrator/desktop/new.py", line 16,inch __add__ the returnself+ Other -File"c:/users/administrator/desktop/new.py", line 16,inch __add__ - returnself+ Other -File"c:/users/administrator/desktop/new.py", line 16,inch __add__ + returnself+ Other - + . A . at . - . -File"c:/users/administrator/desktop/new.py", line 16,inch __add__ - returnself+ Other -File"c:/users/administrator/desktop/new.py", line 16,inch __add__ - returnself+ Other inFile"c:/users/administrator/desktop/new.py", line 16,inch __add__ - returnself+ Other to recursionerror:maximum recursion depth exceeded +>>>
As for why the above must first declare a variable with its own class first (A=new_int (5) ...) For a second?this is because here + has a different meaning there are the int class +, there are some of your own definition of the class +. When you start adding, the system will remove the corresponding addition according to the data type of the variable.That's why you do It (A=new_int (5) ...)
Python ~ ~ Object-oriented tool