Python Learning-Object oriented 3

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.