Python Learning Notes--some common BIF

Source: Internet
Author: User

1 #Some common examples of BIF and usages2 3 #-------------------------------------------------------------------4 #Issubclass Determine if a class is a subclass of another class5 #Issubclass (class,classinfo) ClassInfo can be a tuple of class objects, as long as the class is a subclass of any of the candidate classes, and returns True6 classA:7     Pass8 classB (A):9     PassTen classC: One     Pass A  - Print("whether B is a subclass of a:", Issubclass (B,a))#True - Print("B is a subclass of B:", Issubclass (B,B))#Note: It returns true, that B is a subclass of B the Print("whether B is a subclass of C:", Issubclass (B,C))#False - Print("B is the subclass of a class in a, B, C:", Issubclass (B, (a,b,c)))#True - #------------------------------------------------------------------- -  + #------------------------------------------------------------------- - #Isinstance Determines whether an object is a certain type + #isinstance (Object,classinfo) #第一个参数是实例对象, the second parameter is a tuple of classes or a bunch of class objects A classA: at     Pass - classB (A): -     Pass - classC: -     Pass -  inb =B () - Print("B is a type a:", Isinstance (B,a))#True to Print("b Whether it is of type B:", Isinstance (B,B))#True + Print("whether B is of type C:", Isinstance (B,C))#False - Print("B is one of the types in a, B, C:", Isinstance (b, (a,b,c)))#True the Print("Intentional Error:", Isinstance (B, (a,b,c)))#The first parameter here will always return FALSE if it is not an instance object * #------------------------------------------------------------------- $ Panax Notoginseng #------------------------------------------------------------------- - #hasattr (Object,name) Whether there is a property, name is a string the #getattr (Object,name[,default]) Gets the property, default is a string, and returns when the fetch fails + #setattr (object,name,value) Setting Properties A #delattr (object,name) Delete property the classA: +     def __init__(Self, x=1): -self.x =x $A =A () $ ifHasattr (A,"x"): -     Print("have a") - Print(GetAttr (A,"y","I'm sorry, no y this property")) theSetAttr (A,"y", 10) - Print(A.Y)WuyiDelattr (A,"y") the #------------------------------------------------------------------- -  Wu #------------------------------------------------------------------- - #property uses an object that invokes the function, and the function sets the value of the object's properties.  About #Property (Fget=none,fset=none,fdel=none,doc=none) $ classTest: -  -     def __init__(self,size = 1,count = 2): -Self.size =size ASelf.count =Count +  the     defGetSize (self): -         returnself.size $     defsetSize (self,value): theSelf.size =value the     defdelsize (selfcount): the         delself.size the  -     defGetCount (self): in         returnSelf.count the     defSetCount (self,value): theSelf.count =value About     defDelcount (self): the         delSelf.count the  theA =Property (Getsize,setsize,delsize) +b =Property (Getcount,setcount,delcount) -  theTest =Test ()Bayi Print(test.size)#1 theTEST.A = 100 the Print(test.size)# - - Print(TEST.A)# - -  the Print(Test.count)#2 thetest.b = 200 the Print(Test.count)# $ the Print(test.b)# $ -  the  the #-------------------------------------------------------------------

Python Learning Notes--some common BIF

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.