XYZCODE123
XYZCODE123

Reputation: 372

What is the type of *args if args is not provided

I was trying to implement map function in Python and I came across this:

def map(func, iterable, *args):  
    for args2 in zip(iterable, *args):
        yield func(*args2) 

I wrote down a few tests to check if it's working correctly.

from types import *
print(isinstance(_map(None, None), GeneratorType))
print(list(map(lambda x: x.upper(), 'just a line')) == list(_map(lambda x: x.upper(), 'just a line')))
print(list(map(lambda x,y: x+y, [1,2,3,4], [5,6,7,8])) == list(_map(lambda x,y: x+y, [1,2,3,4], [5,6,7,8])))

But this got me thinking, what is happening here:

print(list(map(lambda x: x.upper(), 'just a line')) == list(_map(lambda x: x.upper(), 'just a line')))

I provided only iterable argument. So in this case:

iterable = 'just a line'
args = Not provided 

What in this case is the value of *args? Is it even an object? What it is? When I'm trying to print values

print(args) - > ()
print(len(args)) -> 0 
print(*args)) -> 
print(len(*args)) ->  TypeError: len() takes exactly one argument (0 given)

Upvotes: 0

Views: 56

Answers (1)

Aniket Tiratkar
Aniket Tiratkar

Reputation: 858

You can figure out the type by using type function, but the main problem here, I think, is that you're treating *args as an object which is certainly not the case.

* is just an operator, so when you want to use function like len or type on it, you should use args without *.

To answer your question, it's always a tuple.

Upvotes: 1

Related Questions