Let's say I have a list
x = [1,2,3,4,5,6,7,8,9,10]
And I want to print this list by calling a function. Here is my function to print the list
def print_list(list_received):
for var in list_received:
print var
And I am calling print_list() like below
print_list(x)
| |
method argument
name
So above function call looks similar to one we used to do in C/C++.
Now lets say I have a list called my_list
my_list = [1,2,3,4,5,6,7,8,9,10]
And I want to convert my_list to tuple & I will be doing like below
my_tuple = tuple(my_list)
Now Here this tuple(my_list) is called typecasting as I read from here http://www.pitt.edu/~naraehan/python2/data_types_conversion.html
now above typecasting looks completely different from C or C++ as in C we used to do
int x = 10;
char ch = (char)x
My first doubt is what is the rational theory or consideration about changing C looks like (tuple)my_list to tuple(my_list) ? Answer may be Python is not C but I want to know more.
My second question is how python interpreter will decides whether its a function call or type conversion function i.e both calling convention of my_print(x) and tuple(my_list) looks similar but actually both are doing different job.
tuple()is also builtin. But can I say here that I am callingtuple()method in statementtuple(my_list)?tuplefunction. It returns a tuple. End of story.Cexplicit typecasting is not consider as function call, isn't it ? In pythonint(x)and in C(int)xboth works same but convention are different, wherexis float variable .int(x)callsx.__int__(), which could beint.__int__,float.__int__,numpy.int64.__int_etc. depending on the type ofx