Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function Default Arguments
#1
Hi Viewer,

Got a query on function default arguments.

i = 10
def fun(l = 10):
    if i is l:
        print('mathced')
    else:
        print('not matched')
fun()
The above code would output "matched".

The same code when used a tuple instead of an integer,

i = (1, 2, 3)
def fun(l = (1, 2, 3)):
    if i is l:
        print('matched')
    else:
        print('not matched')
fun()
prints 'not matched'.

Integers and tuples both being immutable types, why does python differentiate between the both when it comes to default arguments? Thanks in advance.

Thanks,
Kiran.
Reply
#2
Hi, this is odd, I run the very code you posted and get "matched" printed also for the second snippet.
Did you happen to copy/paste/run and redeclared only one object (e.g. i or fun declaration) instead of both, i and fun?
Reply
#3
Hi,

I tried them seperately. And even now cross checked it. I am getting "not matched" in the second case.
Reply
#4
is is identity operator. i.e. it compares that both names point to same object. You should not be using it in this case. Use comparison == in this case.
When it comes to integers read this SO answer
https://stackoverflow.com/a/28864111/4046632

you can print id(i) and id(l) to see if they are same object or not.

You will use is when you want to be certain to names point to exactly the same object. One exception is when you compare to None. You will do i is None, not i == None. That is because None is so called singleton (i.e. exists as object in only one place in memory).
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thanks Buran

I understand the difference between is and == in python. My question was more like even though tuples and integers are immutable types, for integers python decides to use the same object reference and where as for a tuple it creates a new object. Is there some rules or something by which we could know python would create a new object if I use this datatype?

I have noticed the behaviour different for complex numbers as well. For instance,

i = 3+4j
k = 3+4j
print(i is k) 
would print "False" where as

i = 4j
k = 4j
print(i is k)
would print "True".

Thanks
Reply
#6
From the docs
https://docs.python.org/3/c-api/long.html

Quote:The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object.

In fact you can see that it's not only for the range -5, 256. Probably the range depends also on other factors...
Compare
i = 10**2
def fun(l = 100):
    print id(i), id(l)
    if i is l:
        print('matched')
    else:
        print('not matched')
fun()
and

i = 10**3
def fun(l = 1000):
    print id(i), id(l)
    if i is l:
        print('matched')
    else:
        print('not matched')
fun()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
You can test it:
for a, b in zip(*[range(-5, 500)]*2):
    if a is not b:
        print(f'{a} is not {b}')
        break
Output:
257 is not 257
Never use is for numbers.
Otherwise you're relying on an implementation detail of CPython.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
Thanks for taking time to respond.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  calling external function with arguments Wimpy_Wellington 7 1,423 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  'namespace' shorthand for function arguments? shadowphile 5 2,584 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Checking the number of arguments a function takes Chirumer 3 2,150 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Possible to dynamically pass arguments to a function? grimm1111 2 2,164 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  Default values of arguments with the same id DrVictor 3 2,268 Dec-02-2020, 03:27 PM
Last Post: deanhystad
  How to pass multiple arguments into function Mekala 4 2,446 Jul-11-2020, 07:03 AM
Last Post: Mekala
  How to give a name to function arguments in C-API? WonszZeczny 0 1,335 Jun-22-2020, 10:20 AM
Last Post: WonszZeczny
  When Defining a Function with an Equation as a Default Argument, which Value Is Used? OJGeorge4 4 2,650 Apr-09-2020, 08:48 AM
Last Post: DeaD_EyE
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,238 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Pass Arguments to Function phillyfa 2 2,007 Mar-27-2020, 12:05 PM
Last Post: phillyfa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020