Python @ DjangoSpin

Object Oriented Python: Customizing builtin data types

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Customizing builtin data types in Python

Customizing builtin data types in Python

str, int, dict, tuple, list, float, bool, set, are all Python classes. This means that we can subclass them and devise our own classes which behave the same way as the built-in types, and at the same time, give us the option of customize selected functionality.

>>> class MyInt(int):
	def __add__(self, value):
		print("Adding...")
		return int.__add__(self, value)

>>> a = MyInt(5)
>>> a + 6
Adding...
11

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply