4/19 Parameters and Arguments. A question


Just something I’d like to understand. We’re defining a function with two parameters, and which finally accepts two integers as arguments.

What are, in basic terms, the ‘mechanics’ behind being able to type what are basically string values into the function as parameters, and yet the function once defined ‘knows’ that the arguments will be integers?

I find it confusing to think about. Is this simply the way Python has been designed? Are there ever occasions when one would need to specify a type for a function parameter, or does Python just sort all this out by itself?


to truly understand this difference, we need to understand the difference between a static and dynamic language, look at this java function:

void bar(int num1, int num2) {

void simple means the function won’t return anything, then bar is the name of the function, expects two parameters (num1 and num2) of type integer. Here, Java is enforcing data type. This makes it static (the data types for each variable have to be specified)

lets look at another java example:

int myNum = 3;

i need to enforce that nyNum would be of type integer, in python i can simply do:

my_num = 3

python is taking care of assigning the right data type, this is dynamic.

same with parameters in python, python doesn’t enforce data type on the parameters. This has both pros and cons. A pro would be that you could define the parameter, even when you are not yet sure what the data type would be

a drawback would be that if we accidentally pass a string argument, the bug is more difficult to trace (Java would alert you of this, python won’t give it simple will make the parameter a string due to its dynamic typing)

Just for the sake of clarification, Java isn’t the only static typed programming language, and python isn’t the only dynamic language


Thank you stetim94 that was a wonderfully concise and helpful answer.

One further question in light of this:

Since I’m currently learning, a large part of said learning is about working out why the code isn’t working as expected… because when it does work there’s sort of less to think about. Is there an IDE available for Python which throws more detailed errors than I typically get in the web console here, or is this a limitation (as you noted above) that I simply have to accept and live with? I’m far less interested in how quickly I finish each lesson than I am in fully understanding the idea being taught. In this regard, I find the explanatory notes with each lesson to be sometimes woefully lacking.


the point i made about a less obvious error was only referring to the fact that Java would handle an incorrect type as argument as such, while python will throw an error further down the line when something goes wrong inside the function

IDE’s like spyder should give more errors, but in time you will also learn to understand the error messages of the python interpreter better.


ah okay - again many thanks.

I can give an example of the kind of rather frustrating thing right now. Just completed exercise 6/19 by dint of, having tried several versions of my code and getting a simple syntax error: invalid syntax in the console, clicking on get code and by which I learned that my struggles were very simply against the fact that my else: statement was not indented within the function but had simply slipped onto a newline below. Now, as a beginner, this is off-putting because I’m banging my head thinking my code is wrong (yes, technically it is, but semantically it isn’t) had the console been specific about simply a formatting error (which it actually is) I’d find it easier to feel I’m understanding the lesson. Less of the “oh what’s wrong with my thinking” and more “pay attention to white spaces” :wink:

Anyway, I add this simply by way of comment, not meant as a criticism.


i made a simple script:

def example():
    print "hello world"
    print "hello world"

which if i run gives:

python2 projects/temp/temp.py
  File "projects/temp/temp.py", line 3
SyntaxError: invalid syntax

so yes, that is what python gives you. An IDE might indeed help you catch such errors. Not sure, i usually use VIM which doesn’t alert me to such a mistake. (don’t use VIM, its not recommended for beginners)


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.