# Python Question about a function call with multiple arguments

#### Wrichik Basu

Gold Member
2018 Award
Recently in college, we have started learning python. I found that the print statement can accept multiple variables. For example, if I have variables x1, x2 and x3, I can write print(x1, x2, x3) in python. Something similar exists in Matlab as well.

I have learnt Java for nearly four years, and I know that something like this is not possible there. If I have to print three variables, I have to write System.out.println("" + x1 + x2 + x3);, that is, concatenate the variables. I can put multiple arguments only if the function definition allows me to do that. I cannot just put the variables as arguments; rather, I have to concatenate them as a string.

How does python and/or Matlab work in this regard? I put in multiple variables as arguments, and that is accepted! Overloading is certainly not a solution here.

In addition, if I want to define a separator in python print statement, I can mention that using sep = ' ' as an argument in the function call. But how does python recognise this as the separator only using the keyword "sep"?

Related Programming and Computer Science News on Phys.org

#### pasmith

Homework Helper
See the examples here.

#### jedishrfu

Mentor
Also Java hasn't been left behind there is a feature called varargs:

that allows for a variable number of arguments.

The way these schemes work is either the arguments are all of the same type and the compiler creates an array of the arguments under the covers. Or the arguments are of different types and the compiler has a reflection feature ie passes datatype along with the values allowing them to be read back using the correct datatype.

Python in particular has the tuple structure that allows different datatypes to be combined together. This is especially useful when you want to return multiple values ie you return one tuple with the values packed in the returned tuple.

#### Wrichik Basu

Gold Member
2018 Award
Also Java hasn't been left behind there is a feature called varargs:

https://www.geeksforgeeks.org/variable-arguments-varargs-in-java/
that allows for a variable number of arguments.

Our Professor says that the fact that Python doesn't have any syntax to end a structure (unlike Matlab or java, which uses the end keyword and braces respectively), makes it a much more advanced language. Somehow I cannot support this. I know I will not use java for solving a physics problem. But given an option, I will perhaps not use Python as well for doing an extensive program. I would rather prefer Matlab (or Octave). Till now, I knew that the alignment of code is for the purpose of readability of the code. I am seeing for the first time a language that depends completely on alignment.

#### Mark44

Mentor
Also Java hasn't been left behind there is a feature called varargs:
It's surprising to me that this wasn't a part of Java until JDK 5. The varargs concept has been part of C/C++ for many years.

#### PeterDonis

Mentor
How does python and/or Matlab work in this regard?
The Python print function does "under the hood" the string concatenation that Java makes you do explicitly. It's a convenience feature. (Note that Python also accepts arguments to print that are not strings; it calls the __str__ method of those objects under the hood to get their string representation, whereas Java also makes you explicitly call whatever function gives you the string representation of non-string objects before printing.)

Last edited:

#### PeterDonis

Mentor
Somehow I cannot support this.
Having indentation be significant, with no block end delimiters, is something that many people take time to get used to with Python. Having programmed in Python for many years, I find it very useful not to have to type delimiters and keep track of block closures; I can just indent my code the way I would do it anyway, for readability, and Python takes care of the rest.

#### FactChecker

Gold Member
2018 Award

Our Professor says that the fact that Python doesn't have any syntax to end a structure (unlike Matlab or java, which uses the end keyword and braces respectively), makes it a much more advanced language.
IMHO, this is a very superficial reason to call a language more advanced. In fact, Python has some very annoying throwbacks to old problems. For instance, one of the treacherous things that made old makefiles error-prone was the way a continued statement had to end in a '\', but only if there was no space after that. Those were so hard to spot. Of all the things for Python to adopt, that is IMHO the most annoying.

#### jedishrfu

Mentor
I get hit with the tabs vs spaces issue in python as i switch editors based on where the code is needed.

In general its best to have soft tabs where the editor always inserts spaces but you have to remember to check if its on.

#### jedishrfu

Mentor

Our Professor says that the fact that Python doesn't have any syntax to end a structure (unlike Matlab or java, which uses the end keyword and braces respectively), makes it a much more advanced language. Somehow I cannot support this. I know I will not use java for solving a physics problem. But given an option, I will perhaps not use Python as well for doing an extensive program. I would rather prefer Matlab (or Octave). Till now, I knew that the alignment of code is for the purpose of readability of the code. I am seeing for the first time a language that depends completely on alignment.
I would reconsider that argument as python has oo features and can be divided into modules.

The only gripe we have with python is its inability to package everything needed into one file short making a multimegabyte docker image. Of course small scripts with external modules like numpy pandas or whatever can be a single script. Its the application level code that runs into this issue.

#### Wrichik Basu

Gold Member
2018 Award
I get hit with the tabs vs spaces issue in python as i switch editors based on where the code is needed.

In general its best to have soft tabs where the editor always inserts spaces but you have to remember to check if its on.
Spaces and tabs are really creating a problem. Till now, I have mostly used an IDE for writing codes. When I used notepad/gedit, I used to indent with tab. Now in python, this is becoming a major problem. When you are writing a long program, it becomes really irritating to count and put spaces. One space less, and your program falls through.

#### Wrichik Basu

Gold Member
2018 Award
I would reconsider that argument as python has oo features and can be divided into modules.

The only gripe we have with python is its inability to package everything needed into one file short making a multimegabyte docker image. Of course small scripts with external modules like numpy pandas or whatever can be a single script. Its the application level code that runs into this issue.
Yes, it has, but I believe Python is not quite built for the same purpose as java or c++. While learning from online tutorials, I found it is customary to put a reference to the currently called object in a function definition, through the keyword self. I don't remember doing this in java (except maybe in a few cases). I believe Python might be good for scientific computing, but not suitable for application development.

#### Wrichik Basu

Gold Member
2018 Award
For instance, one of the treacherous things that made old makefiles error-prone was the way a continued statement had to end in a '\', but only if there was no space after that.
Does this problem still exist in Python 3.7, or has it died away with 2.7?

An additional problem of python is compatibility issues. Currently, I am coding in 3.7, but who knows, they might stop supporting codes of 3.x once 4.x is released.

#### PeterDonis

Mentor
When you are writing a long program, it becomes really irritating to count and put spaces.
Every editor I'm aware of allows you to let the "Tab" key be the equivalent of some standard number of spaces (4 is the usual number for Python code).

#### PeterDonis

Mentor
I believe Python might be good for scientific computing, but not suitable for application development.
If this were true, it would be a huge surprise to the many people (myself included) who develop applications in Python.

Since you admit you are unfamiliar with Python as a language, you should be more cautious in your claims.

#### PeterDonis

Mentor
While learning from online tutorials, I found it is customary to put a reference to the currently called object in a function definition, through the keyword self.
It's not "customary", it's required in Python. If a function is a method on a class, it has to have as one of its arguments the instance of that class that is calling the method. Other languages like C++ do this by an implicit parameter ("this" in C++) that is automatically defined inside methods. Python does it by explicitly requiring you to define the "self" argument when you define the function. That way an individual function doesn't even have to "know" whether it's a method on a class or not; it just operates on the arguments that are passed to it.

#### PeterDonis

Mentor
Does this problem still exist in Python 3.7, or has it died away with 2.7?
AFAIK it's still there. However, use of continuations of lines like this is extremely rare in Python (whereas it was common with makefiles). And there's an easy way to avoid it in Python (whereas there was no way to avoid it in makefiles): just enclose any expression that will occupy multiple lines in parentheses, since that makes the expression look like one line to the interpreter. This is commonly done, for example, with long import statements.

#### PeterDonis

Mentor
An additional problem of python is compatibility issues. Currently, I am coding in 3.7, but who knows, they might stop supporting codes of 3.x once 4.x is released.
Considering that Python 2.7 is still supported almost 10 years after Python 3 was initially released, I think this concern is extremely farfetched.

#### Wrichik Basu

Gold Member
2018 Award
If this were true, it would be a huge surprise to the many people (myself included) who develop applications in Python.

Since you admit you are unfamiliar with Python as a language, you should be more cautious in your claims.
Well, I do not claim this. I admit that I am a beginner. Our Professor told us in the class that python is not quite used for application development.

#### Wrichik Basu

Gold Member
2018 Award
Considering that Python 2.7 is still supported almost 10 years after Python 3 was initially released, I think this concern is extremely farfetched.
Correct me if I am wrong, but I think there are some compatibility issues if you try to execute a python 2.7 program using 3.7 compiler (at least our Prof. told so; I am not sure exactly where these issues occur).

#### PeterDonis

Mentor
I do not claim this.
Do not claim what? You posted this:

I believe Python might be good for scientific computing, but not suitable for application development.
That's a claim. You said "I believe". That's stating it as your opinion, not someone else's.

Our Professor told us in the class that python is not quite used for application development.
And if that's what you meant, that's what you should have said. My response would be much the same.

#### PeterDonis

Mentor
Correct me if I am wrong, but I think there are some compatibility issues if you try to execute a python 2.7 program using 3.7 compiler
Yes, which is why you use a Python 2.7 interpreter to run Python 2.7 code. Python 2.7 is available on any system where Python 3.7 is available (in fact, Python 2.7 is probably available on more systems than Python 3.7 since Python 3.7 is a very recent release). So this is not a problem.

#### FactChecker

Gold Member
2018 Award
Yes, which is why you use a Python 2.7 interpreter to run Python 2.7 code. Python 2.7 is available on any system where Python 3.7 is available (in fact, Python 2.7 is probably available on more systems than Python 3.7 since Python 3.7 is a very recent release). So this is not a problem.
That is not true. In a large organization, where a system administration department takes care of all computers, it can be a constant struggle to get the right ones on your computer and on other computers that the code needs to be developed on. Where I worked, it took an act of God to get the right things on the computers.

#### PeterDonis

Mentor
In a large organization, where a system administration department takes care of all computers, it can be a constant struggle to get the right ones on your computer and on other computers that the code needs to be developed on.
Yes, but it's just as much of a struggle to get Python 3.7 on them all as it is to get Python 2.7 on them all. So this is not an advantage of Python 3.7 over 2.7.

#### PeterDonis

Mentor
In a large organization, where a system administration department takes care of all computers, it can be a constant struggle to get the right ones on your computer and on other computers that the code needs to be developed on.
Also, this problem is hardly particular to Python; all languages have new versions come out and have backwards compatibility issues with old versions. Python supporting 2.7 for almost 10 years now since Python 3 was released is actually a pretty long support time frame for an old version, compared to other languages.

"Question about a function call with multiple arguments"

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving