Python @ DjangoSpin

Python: Duck Typing

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

Duck Typing in Python

Duck Typing in Python

Python allows the user to skip the data type of variables. It doesn't even require you to declare the return type of functions. This is in contrast to statically typed languages such as C++ & Java. This behaviour of Python is based on the Duck Test, which states that "If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck." Python recognizes the variables on the basis of values they hold.

>>> a = 'hi'
>>> type(a)
<class 'str'>
>>> a = 5
>>> type(a)
<class 'int'>

See also:

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

Leave a Reply