1

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.

8
  • 1
    but type conversion functions are also functions Commented Jun 4, 2018 at 9:57
  • Thanks @Ev.Kounis yes tuple() is also builtin. But can I say here that I am calling tuple() method in statement tuple(my_list) ? Commented Jun 4, 2018 at 10:01
  • There's no difference between a function call and a type conversion function. You call the tuple function. It returns a tuple. End of story. Commented Jun 4, 2018 at 10:02
  • But Normally in C explicit typecasting is not consider as function call, isn't it ? In python int(x) and in C (int)x both works same but convention are different, where x is float variable . Commented Jun 4, 2018 at 10:04
  • 1
    In python, int(x) calls x.__int__(), which could be int.__int__, float.__int__, numpy.int64.__int_ etc. depending on the type of x Commented Jun 4, 2018 at 10:13

3 Answers 3

3

my_tuple = tuple(my_list) creates an instance of the class tuple from my_list object. It's not quite what a type casting like (tuple)my_list does in C since a new object is created.

my_tuple = tuple(my_list) is closer to the C++ my_tuple = new Tuple(my_list).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. can you tell me path or source of tuple class implementation or python standard where I can see the implementation.
This is the tuple documentation and the source is here
Thank you very much. This is what I needed.
2

Why it should be identified as a normal function or type conversion function? It is just a function.

Or as almost everything in python is an object, actually function is also a kind of object which implements __call__.

Further, there isn't a type conversion function in fact. tuple acts as an constructor, it takes an iterable object and return a tuple object.

5 Comments

yes I agree with on tuple being returning tuple object. But internally how it will initialize/convert list into tuple ? by calling some methods like constructor of the class ? correct me if I am wrong ?
Like Jacques Gaudin says, it does work like a constructor. It can convert not only list but any object which is iterable.
it does not work "like" a constructor: tuple is a class
yes @bobrobbob tuple is a class but __init__() method of tuple class will initialize it.
sorry, i was just being pedantic to avoid misunderstanding. it is not like a constructor, it is a constructor
0

I may answer your second question, Python interpreter works on the basis of scope (local,enclosing,global,default).

for example:

def tuple(l_list):
     print l_list

l_list = [1,2]
tuple(l_list)

In above case it doesn't do type conversion instead it executes above function. Why because once we started execution it searches for tuple object and does job. If it didn't find then it considers default keyword and converts. In nutshell It is purely on scope of objects

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.