Question about a function call with multiple arguments

  • Context: Python 
  • Thread starter Thread starter Wrichik Basu
  • Start date Start date
  • Tags Tags
    Function Multiple
Click For Summary

Discussion Overview

The discussion revolves around the behavior of function calls with multiple arguments in Python and Matlab compared to Java, particularly focusing on the print function in Python and the handling of variable arguments in Java. Participants explore the implications of these differences on programming practices and preferences.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants note that Python's print function can accept multiple variables directly, unlike Java, which requires concatenation of variables into a single string for printing.
  • Others mention Java's varargs feature, which allows for a variable number of arguments, and discuss how it operates with type handling.
  • There is a discussion about Python's use of indentation for block structures, with some participants expressing difficulty in adapting to this style compared to languages with explicit block delimiters.
  • Some participants express a preference for Matlab or Octave over Python for extensive programming tasks, citing concerns about Python's reliance on indentation and its handling of modules.
  • Concerns are raised about compatibility issues in Python, particularly regarding the transition from Python 3.x to potential future versions.
  • Participants discuss the challenges of managing indentation in Python, especially when switching between editors that handle tabs and spaces differently.

Areas of Agreement / Disagreement

Participants express a mix of opinions regarding the advantages and disadvantages of Python's syntax and structure compared to Java and Matlab. There is no consensus on which language is superior for specific tasks, and multiple competing views remain regarding the usability and suitability of Python for different programming contexts.

Contextual Notes

Some participants highlight limitations in Python's design, such as issues with statement continuation and the potential for compatibility problems in future versions. These points remain unresolved and are subject to individual interpretation.

Wrichik Basu
Science Advisor
Insights Author
Gold Member
Messages
2,186
Reaction score
2,694
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 learned 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"?
 
Technology news on Phys.org
See the examples here.
 
  • Informative
Likes   Reactions: Wrichik Basu
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.

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.
 
  • Informative
  • Like
Likes   Reactions: QuantumQuest, Klystron and Wrichik Basu
jedishrfu said:
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.
Didn't know about that. Thanks.

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.
 
  • Like
Likes   Reactions: Klystron
jedishrfu said:
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.
 
Wrichik Basu said:
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:
Wrichik Basu said:
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.
 
  • Like
Likes   Reactions: Wrichik Basu
Wrichik Basu said:
Didn't know about that. Thanks.

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.
 
  • Like
Likes   Reactions: Wrichik Basu
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.
 
  • #10
Wrichik Basu said:
Didn't know about that. Thanks.

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.
 
  • #11
jedishrfu said:
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.
 
  • #12
jedishrfu said:
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.
 
  • #13
FactChecker said:
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.
 
  • #14
Wrichik Basu said:
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).
 
  • #15
Wrichik Basu said:
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.
 
  • #16
Wrichik Basu said:
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.
 
  • #17
Wrichik Basu said:
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.
 
  • #18
Wrichik Basu said:
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.
 
  • #19
PeterDonis said:
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.
 
  • #20
PeterDonis said:
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).
 
  • #21
Wrichik Basu said:
I do not claim this.

Do not claim what? You posted this:

Wrichik Basu said:
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.

Wrichik Basu said:
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.
 
  • #22
Wrichik Basu said:
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.
 
  • #23
PeterDonis said:
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.
 
  • #24
FactChecker said:
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.
 
  • #25
FactChecker said:
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.
 
  • #26
Wrichik Basu said:
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.
Java has self embedded as the this pointer which is optionally used when a naming conflict occurs between function args and instance attributrs
 
  • #27
jedishrfu said:
Java has self embedded as the this pointer which is optionally used when a naming conflict occurs between function args and instance attributrs

For python oo is kind of tacked on to the language hence you need to use self in methods as a placeholder argument but not when you call the method.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 43 ·
2
Replies
43
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K