This article mainly introduces the method of Python to extend the built-in type, and analyzes the concrete implementation techniques of Python embedding built-in type extension and subclass mode extension with instance form, and the friend can refer to the following
The example in this article describes how Python implements the extended built-in type. Share to everyone for your reference, as follows:
Brief introduction
In addition to implementing new types of object methods, we can sometimes extend Python's built-in types to support other types of data structures, such as increasing the queue's insertion and deletion methods for lists. In this paper, we introduce the two methods of extending Python's built-in type by embedding built-in types and extending the type through subclasses, with examples of implementing the collection function.
By embedding a built-in type extension
The following example implements a collection object by using the list object as an embedded type, and adds a bit of operator overloading. This class of knowledge wraps up a list of Python, along with additional set operations.
Class Set:def __init__ (self, value=[]): # Constructor self.data = [] # manages a list self.concat (value) def inte Rsect (self, Other): # all sequence res = [] # Self was the subject for X in Self.data:if x in other: # Pick Common Items res.append (x) return set (RES) # return a new set Def union (self, Other): # Other was any sequ ence res = self.data[:] # Copy of my list for x in other: # ADD items in and if not X in Res:res.appe nd (x) return Set (RES) def concat (self, value): # Value:list, Set ... for x in value: # removes duplicates if n OT x in Self.data:self.data.append (x) def __len__ (self): return Len (self.data) # len (self) def __getitem__ (s Elf, Key): Return Self.data[key] # Self[i] def __and__ (self, Other): Return Self.intersect (Other) # Self & other de F __or__ (self, Other): Return Self.union (Other) # Self | Other def __repr__ (self): return ' Set: ' + repr (self.data) # print () if __name__ = = '__main__ ': x = Set ([1, 3, 5, 7]) print (X.union (Set ([1, 4, 7])) # prints Set:[1, 3, 5, 7, 4] Print (x | Set ([1, 4, 6]) # prints set:[1, 3, 5, 7, 4, 6]
To extend a type by way of a subclass
Starting with Python2.2, all built-in types can directly create subclasses such as list,str,dict and tuple. This allows you to customize or extend the built-in type with user-defined class statements: Create subclasses of type names and customize them. A subtype instance of a type that can be used anywhere the original built-in type could appear.
Class Set (list): Def __init__ (self, value = []): # Constructor list.__init__ ([]) # customizes list self.co NCAT (value) # Copies mutable defaults def intersect (self, Other): # all sequence res = [] # Self are the subject for X in Self:if x in other: # Pick Common Items res.append (x) return Set (res) # Return a new Set def union (Self, Other): # sequence res = set (self) # Co py me and my list res.concat (other) return res def concat (self, value): # Value:list, Set ... for x in Value: # removes duplicates if not X in Self:self.append (x) def __and__ (self, Other): Return self.in Tersect (Other) def __or__ (self, Other): Return Self.union (All) def __repr__ (self): return ' Set: ' + list.__repr__ (SE LF) If __name__ = = ' __main__ ': x = set ([1,3,5,7]) y = set ([2,1,4,5,6]) print (x, y, Len (x)) print (X.intersect (y), Y.unio N (x)) print (X & y,x | Y) x.reverse (); Print (x)