Object-oriented Other related one, isinstance (object,class_or_type_or_tuple)
Object that checks if object is Class_or_type_or_tuple
class Foo (object): Pass = Foo ()if isinstance (obj,foo): print('Yes ' ' )else: print('No')
Second, Issubclass (c,b)
Check if C is a subclass of B
class B (object): Pass class C (B): Pass Print (Issubclass (C, B))
Third, super ()
Methods to execute the parent class
instances, ordered dictionaries
classmydict (dict):def __init__(self): self.__li=[] Super (mydict,self).__init__() def __setitem__(self, Key, value): Self.__li. Append (key) Super (mydict,self).__setitem__(key, value)def __str__(self): temp_list= [] forKeyinchSelf.__li: Temp_list.append ("\ '%s\ ':%s"% (key,self.__getitem__(key))) Temp_str="{"+",". Join (Temp_list) +"}" returnTemp_strobj=mydict () obj['K1'] ='v1'obj['K2'] ='v2'Print(obj)
Iv. Exception Handling
Exception handling for all structures, including at least try...except ...
Try: #Line-by- row execution of the main code snippet Print(123)exceptValueError as ex:#executed when ValueError is detected Print(ex)exceptException as ex:#Exception matches all the same #executes when an error other than ValueError is detected Print(ex)Else: #executes when the main code block finishes executing Passfinally: #regardless of whether the main code block has an error, execute Pass
Proactively triggering exceptions
Try : Raise Exception (' active error ')except Exception as E: Print (e)
Custom exceptions
class MyException (Exception): Span style= "color: #0000ff;" >def __init__ (self, msg): Self.message /span>= msg __str__ return Self.message try : raise myexception ( " proactive wrong custom error ) MyException as E: print (E)
Assertion
# report assertionerror error when expression after assert is True assert 1 = = 1assert 1 = = 2
V. Design model of a single case model
Simple interest mode exists to ensure that only a single instance exists in the current memory, avoiding memory waste
classConnectpool (object):__pool=Nonedef __init__(self):Pass@staticmethoddefGet_pool ():if notConnectpool.__pool: Connectpool.__pool=Connectpool ()returnConnectpool.__poolobj1=Connectpool.get_pool () obj2=Connectpool.get_pool ()Print(OBJ1,OBJ2)
Python Learning-Object oriented 3