A Data Science Central Community

Function and Polymorphism

When I started learning these two languages, as mentioned before, I always keep in mind that the difference between dynamic data type and static data type. Then the question came out: why they need to be like that? What is the advantage of being dynamic data type or being static data type? To make the question more concrete, let’s see how to define a function in both languages.

In Java, everything is very straight forward:

The definition of a function will always contain its returning data type(void if nothing to be returned). And also, the type of the parameters will always be declared. When calling the function if the parameters input are not of the right type, then exception will be raised.

In Python, everything is simple, yes, simple:

__def__ ** funtionName**(paraName1, paraName2):

__function__ __body__

return returnValue(optional if you __need__ __to__ return __something__)

__ __

As always stated, there is no data type declaration. Namely, you can input whatever you want as long as the function body action support. And also, you don’t need to state in the header whether the function is returning something and what it will return. You can even set the default value for each parameter which is not supported in Java. A simpler and clearer coding, but for me, it is messier than Java, because it is not safe. You will never know for sure whether the function is working properly. What if you input some parameters of a wrong type? Most of the time the function will work and return something unexpected.

What is the pro for this kind of coding style? This is the very question hovering above my mind for a very long time. I asked my professor (who teaches me Java) and she said: “go ask the person who invent it, I don’t know. For me Python is a mess.” When I asked someone who is introduced to me as an expert in Python and machine learning, I got no answer. She stopped contacting me since I asked this question. Then I knew I asked a very stupid question, though I didn’t know the answer.

When I learned the term “Guardian”, I thought I found the answer. The definition of guardian is as follow:

“A programming pattern that uses a conditional statement to check for and handle circumstances that might cause an error.”

Then the statements of defining a function in Python will become:

def ** funtionName**(paraName1, paraName2):

if not isinstance(paraName1, int) or not isinstance(paraName2, int):

print *"error message."*

return None

else:

__function__ __body__

return __returnValue__ *@optional if you need to return something *

The guardian is useful when you want to have the full control on the input data type and the output. It will make the code in Python similar to that in Java.

But still, this is not the answer to my hovering question. Python is not invented to act with things complicated to mimic Java.

I keep on searching and learning. Then I learned the concept ‘polymorphism’, and a term ‘generic function’. I know that I find the answer. A generic function is a function whose behavior depends on the type of the attributes input as parameters.

In Java, a generic function is a set of functions with the same name but different formal parameters.

For example:

** ****public** **static** **int** testF(**int** a, **int** b){

**return** a+b;

}

**public** **static** **boolean** testF(**boolean** a, **boolean** b){

**return** a||b;

}

When you call the testF function like this

A=testF(2,3);

Then A is 5. But :

boolean A = testF(true, false);

then A is ture.

The static function testF() is a generic function.

But as you can see, testF() is actually two different functions. If we want a function with a hundred of parameter combinations but only one name, then we have to write 100 different codes to define the function. Is it possible that we write only one stack of statements to define a function with 100 possible parameter combinations? This is where the idea ‘polymorphism’ came from.

Polymorphism is the provision of a single interface to entities of different types.

In Java, polymorphism is quite a high level topic which is beyond the scope of this article. It contains 3 subclass:

- Function overloading (as shown above)
- Parametric polymorphism
- Subtyping and inheriting

For more detail you need a text bookJ.

Python is built to be polymorphic. You can easily write parametric polymorphic functions in Python while it’s quite complicated to do this in Java. You cannot overload functions, but by setting the default value and the guardian statements you can do the same thing. Python is an OOP language, thus subtyping and inheriting is also achievable.

Think about the primitive type in Python: int, long, float, boolean, complex, tuple, list, dictionary.

If only for data analysis, you don’t even need to write a new class. All the above are already enough. Integrating the ease of writing generic function, Python is really convenience for data processing and calculation. But you don’t want to use Python to develop some large scale enterprise applications (as my professor said it would be a mess if you did so).

Contrary, compare to Python, java is more suitable to develop large scale software. But for data analysis, you have to work a lot before you take action.

And after all, my question is solved.

© 2019 AnalyticBridge.com is a subsidiary and dedicated channel of Data Science Central LLC Powered by

Badges | Report an Issue | Privacy Policy | Terms of Service

**Most Popular Content on DSC**

To not miss this type of content in the future, subscribe to our newsletter.

- Book: Statistics -- New Foundations, Toolbox, and Machine Learning Recipes
- Book: Classification and Regression In a Weekend - With Python
- Book: Applied Stochastic Processes
- Long-range Correlations in Time Series: Modeling, Testing, Case Study
- How to Automatically Determine the Number of Clusters in your Data
- New Machine Learning Cheat Sheet | Old one
- Confidence Intervals Without Pain - With Resampling
- Advanced Machine Learning with Basic Excel
- New Perspectives on Statistical Distributions and Deep Learning
- Fascinating New Results in the Theory of Randomness
- Fast Combinatorial Feature Selection

**Other popular resources**

- Comprehensive Repository of Data Science and ML Resources
- Statistical Concepts Explained in Simple English
- Machine Learning Concepts Explained in One Picture
- 100 Data Science Interview Questions and Answers
- Cheat Sheets | Curated Articles | Search | Jobs | Courses
- Post a Blog | Forum Questions | Books | Salaries | News

**Archives:** 2008-2014 |
2015-2016 |
2017-2019 |
Book 1 |
Book 2 |
More

**Most popular articles**

- Free Book and Resources for DSC Members
- New Perspectives on Statistical Distributions and Deep Learning
- Time series, Growth Modeling and Data Science Wizardy
- Statistical Concepts Explained in Simple English
- Machine Learning Concepts Explained in One Picture
- Comprehensive Repository of Data Science and ML Resources
- Advanced Machine Learning with Basic Excel
- Difference between ML, Data Science, AI, Deep Learning, and Statistics
- Selected Business Analytics, Data Science and ML articles
- How to Automatically Determine the Number of Clusters in your Data
- Fascinating New Results in the Theory of Randomness
- Hire a Data Scientist | Search DSC | Find a Job
- Post a Blog | Forum Questions

## You need to be a member of AnalyticBridge to add comments!

Join AnalyticBridge