in-depth learning of classes
A. all things in Python are objects
B. class Foo:
Pass
obj = Foo ()
# obj is an object, Foo class
# Foo class is also an object, type objects
c. classes are objects of type class type (..)
"Object" is a Class object class ()
D. class is actually an object of type types, all classes are subclasses of object
Methods for creating a class [2 kinds]
# First: The class is actually an object of type types, all classes are subclasses of object foo = type (' foo ', (object,), {' Func ': ' function '}) # Second: Class Foo: def func ( Self): print (123) F = Foo () f.func ()
Create a class with Metaclass: You must inherit the type class, and Init must pass 4 arguments past
You must inherit the type class ---code is wrong---class MyType (type): # The Metaclass of a derived class must be a (non-strict) subclass of the meta Classes of all its bases # must give 4 parameters def __init__ (self, *args, **kwargs): # __init__ () takes 1 positional a Rgument but 4 were given print (' MyType Create Class object ') def __call__ (self, *args, **kwargs): print (' MyType's Call method ') def __new__ (self, *args, **kwargs): print (' MyType's New method ') class Foo (object, Metaclass =mytype): def func (self): print (123) # Executes the Init method after creating the object Def __new__ (self, *args, **kwargs): Print (' Foo's New method ') returns ' The object of foo ' F = foo () # call MyType's __init__ method, Foo is the MyType object, and foo () calls MyType __call_ _ Method F.func () # MyType Create Class object, here is MyType # 123
Schematic diagram of the "Reprint" class:
Exception Handling
Passive exception
Try: passexcept indentationerror as E: passexcept valueerror as E: # Small exception put in front of exception Passexcept Exception as E: # E is a Exception object that encapsulates the Exception information passelse: # Normal code is normal, then else is executed, otherwise else passfinally: Pass # Error not error, be sure to execute the code
Active Trigger exception: raise Exception ("Sorry")
Try: raise Exception ("Sorry") except Exception as E: print (e)
Custom Exception: inherits the exception class to implement
Class HhH (Exception): def __init__ (self, msg): self.message = msg def __str__ (self): return Self.message # Here you just need to return it, can't print try: raise HhH (' HhH, wrong ') except HhH as E: print (e)
assert assert
Assert comparison content: The condition is established, then print XXX, otherwise error
Generally with mandatory user's obedience, Java has added this feature since 1.2, but it is not usually used in practice. Python source code useful to
Assert 1 < 5print (' HHH ')
Reflection
1. Manipulating the members of an object via a string (method, field):
Class Foo: def __init__ (self, Name, age): self.name = name Self.age = Age def fun (self): print ('%s- %s '% (Self.name, self.age)) obj = Foo (' FTL ', total) print (obj.name) b = ' name ' Print (' obj.__dict__[b]: ', obj.__dict__[b]) # Value Print via dictionary ("GetAttr (obj, ' name '):", GetAttr (obj, ' name ')) # Fetch value via built-in function getattr = getattr (obj, ' fun ') fun () SetAttr (obj, ' school ', ' Xupt ') print (hasattr (obj, ' School ')) print (Delattr (obj, ' School '))
Module-Level Reflection:
Class Foo (): NAME = ' FTL ' def hello (self): print (' Hello ') print (GetAttr (foo, ' NAME ')) Hello = GetAttr (foo () , ' Hello ') # takes the memory address of the function print (hello) print (hello ()) # takes the object to the function
single-case mode
Class Foo: __instance = None def __init__ (self, Name, age): Self.age = age self.name = name @ Classmethod # static method def get_instance (CLS): if cls.__instance: return cls.__instance else: Cls.__instance = Foo (' hhh ', +) return cls.__instance def show (self): print (self.age, self.name) obj = Foo.get_instance () obj.show ()
"Learn More"
Course Selection System
Object-Oriented programming more references
Python Learning---Object-oriented learning [in-depth]