+ 3

What makes [a, b, c ... ] a list?

To create/get an object/instance of a user-defined class we do: <obj_name> = <class>([<some_object>]) And the same is true for built-in classes: E.g., a = int(), a = int(2.7) etc. But what makes a mere integer also an int object? In general, how do these become an instance of respective classes? 5 int 3.2 float [8, 1] list etc. And to my knowledge only built-in classes support this way to create objects.

30th Jul 2021, 7:20 AM
abhinav
abhinav - avatar
5 Respostas
+ 5
The system knows about its kernel built-in s. The best fitting is chosen. but it cant guess user classes. if u want this behaviour, you could try to work with factory classes.
30th Jul 2021, 7:26 AM
Oma Falk
Oma Falk - avatar
+ 1
In python we have type() function to get type of input we have given
30th Jul 2021, 7:41 AM
Sâgærāvürï
Sâgærāvürï - avatar
+ 1
abhinav Yup, the interpreter knows that 12 is an object of the int class. This is a built-in behaviour. So 12 isn't actually an int but a reference to an int object containing the value '12' (I guess that class properties make this possible). You can check this out with the following code: print(id(12)) a = 12 print(id(a))
10th Aug 2021, 5:11 PM
Calvin Thomas
Calvin Thomas - avatar
0
Thanks Oma Falk I believe it's related to the (conversions made by) interpreter. But what are factory classes?
30th Jul 2021, 7:31 AM
abhinav
abhinav - avatar
0
You're right Sâgærāvürï but what I'm asking is completely different
30th Jul 2021, 7:47 AM
abhinav
abhinav - avatar