Author | Message |
---|---|
asalin14
Posts: 12
|
Posted 11:22 Mar 25, 2019 |
In class, I mentioned an issue with extending type in Python. You would think that if a class extends type, it would take all of its functions. We can see this is partially true.
According to the type documentation, the type class' constructor can do two things: One Argument: When running type(one_arg), type usually returns the .__class__ value of that argument. Three Arguments: When running type with 3 arguments, you must include (class_name, bases, dict). This is what is ran when creating a new class with metaclass type. A valid example call would be type('Squre', (Shape), {length: 1}). You would think extending type would allow you to run both of these functions but Python only allows you to run this with three parameters. I have attached a Python file showing this interaction.
In the type documentation (https://docs.python.org/3/library/functions.html?highlight=type#type), it addresses this issue:
Does anyone have any ideas on why this change might have been made? I suspect that type was implemented in C. I don't think it is possible to restrict inheriting functions when implementing functions in Python. I think it would help to find the implementation in the Python source but I'm not sure where to start.
|
rabbott
Posts: 1649
|
Posted 12:01 Mar 25, 2019 |
Your attachment has this code. See added comments.
Last edited by rabbott at
12:28 Mar 25, 2019.
|
asalin14
Posts: 12
|
Posted 12:53 Mar 25, 2019 |
Both are calling the same __new__ method of type/Road. (Not __init__ as I claimed earlier) Last edited by asalin14 at
13:00 Mar 25, 2019.
|
rabbott
Posts: 1649
|
Posted 16:57 Mar 25, 2019 |
Another bit of information.
My guess is that this means that In other words, depending on how As Abel pointed out, this is not consistent with standard Python syntax. Normally one can't use the same name for two functions, even if the two functions have different signatures. It's been a while since I used Java, but if I'm remembering correctly, one can use the same name for different functions in Java. How about C/C++ ? Since Python is implemented in C++ (or is it straight C?) can use use the same name for multiple functions in C? Last edited by rabbott at
17:00 Mar 25, 2019.
|
asalin14
Posts: 12
|
Posted 18:09 Mar 25, 2019 |
I finally found the type.__new__ implementation (type_new): https://github.com/python/cpython/blob/3.7/Objects/typeobject.c#L2349
The issue is addressed:
In the code, we can see that it differentiates the different functions (type(X) and type(str, bases, dict)) by counting the number of arguments. It only allows the one argument when the type passed in is exactly a PyType_Type.
The reason for this implementation is explained in this link: My understanding is that checking if the passed in class is a subclass of type every time the type function is called is a waste of resources. Since type is called any time a class object is created, Python could be more efficient by only accepting the one parameter call when the type is exactly type (since it would not have to also check if it is a subclass of type). Last edited by asalin14 at
18:11 Mar 25, 2019.
|
rabbott
Posts: 1649
|
Posted 18:18 Mar 25, 2019 |
Last edited by rabbott at
18:24 Mar 25, 2019.
|