How can I open the underlying script of built-in functions? [duplicate] - python

This question already has answers here:
Finding the source code for built-in Python functions?
(8 answers)
Closed 1 year ago.
How can I open the underlying script for built-in functions? For example the function len.
I want to open the script of the len function to study how the underlying code is built, for educational purposes.
I have tried writing open(len) and open(len()), but I only get this error message:
Traceback (most recent call last):
File "C:/Users/someo/PycharmProjects/Test/test.py", line 1, in <module>
open(len())
TypeError: len() takes exactly one argument (0 given)

These definitions can't be accessed, because they aren't part of a package. They live in Python itself, therefore they cannot be accessed. You can't get in by making them raise an error either (len(5) won't let you see inside the function, and will just raise a TypeError). Python itself won't show you the full traceback because these definitions aren't meant to be seen.

Related

require function for parameter python [duplicate]

This question already has answers here:
How can I specify the function type in my type hints?
(5 answers)
Closed 1 year ago.
So I decided I wanted to make my own custom event system thing and it all works except that on line 2 python is like:
Traceback (most recent call last):
File "d:\CollidaCube\VSCode\GameAPY\event.py", line 1, in <module>
class EventListener():
File "d:\CollidaCube\VSCode\GameAPY\event.py", line 2, in EventListener
def __init__(self, func: function):
NameError: name 'function' is not defined
why is this and how can i fix it? my code
Seon has responded with a valid answer to my question. This has been answered here. In my case, I have decided to use type(abs). its simple and I understand it.
Edit:
After some experimentation I discovered that my VSCode nor python will yell at me if I do type[abs].

'str' object is not callable, but I'm not using "str" at all [duplicate]

This question already has answers here:
TypeError: 'str' object is not callable with input() [duplicate]
(4 answers)
Closed 3 months ago.
I'm using Google Colab for development. My script only has one line of code where I'm supposed to read input from the console:
question = input("Hello")
But it is throwing this error: 'str' object is not callable
I search for similar problems and all of them where related to code using "str" as a variable or function name so it needed to be renamed, but in my case it is literally one line of code and I'm not using "str" at all.
Here is a screenshot of Google Colab:
I tried this in a brand new notebook and it worked. In your notebook it appears that input is not the built-in function to read user input but is actually a string of some sort.
To debug this further, try changing the code to just say input. Then it will print what the input identifier is bound to. You ought to see something like <bound method Kernel.raw_input of <google.colab._kernel.Kernel object at 0x7f9fb5f40210>>...
...but I suspect you'll see something else.
somewhere above you have
input = something_that_is_a_string
and that is shadowing the builtin input
dont name your variables input
You have a string variable named input previously in your code. Or perhaps you previously had such a variable and deleted the code, but the variable is still in the namespace. Just restart your Google Colab environment from the top menu: Runtime --> Restart Runtime. That will clear out all variables from memory and let you start over.
try this before your code.
del(input)

weird * in function definition [duplicate]

This question already has answers here:
Bare asterisk in function parameters?
(6 answers)
Closed 4 years ago.
I met a weird case in the function definition in python, I read some code like this:
def abc(dd, *, ee=None):
print(dd, ee)
In the beginning, I thought this code is wrong and maybe a typo of *args, but recently I tried this code in latest python3.7, and it seems that it can be interpreted, and usage is also super wired, you can't pass more than 1 argument to this function:
>>> abc(11, 222)
Traceback (most recent call last):
File "<input>", line 1, in <module>
abc(11, 222)
TypeError: abc() takes 1 positional argument but 2 were given
>>> abc(11)
11 None
I am asking because I don't know the purpose of why some one wrote like this, and why python support this behaviour in Python3(not supported in python2)
It seems your function has 1 positional argument and one named argument. The * eats all other positional arguments, just like an *args would, but you cannot reference them.

Dll functions' names error when using ctypes [duplicate]

This question already has answers here:
C++ dll called from Python
(3 answers)
Closed 4 years ago.
I am given a DLL that was made by another person and I can't access its original .cpp and .h file. Based on a description file that describes the DLL functions' names, I am going to call them with Python using ctypes:
from ctypes import *
cppdll = WinDLL("C:\\VS_projects\\MusicServer_Flask\\NetServerInterface.dll")
py_InitNetwork = cppdll.InitNetwork
However, I failed to call these functions with this Error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python35-32\lib\ctypes\__init__.py", line 360, in __getattr__
func = self.__getitem__(name)
File "C:\Python35-32\lib\ctypes\__init__.py", line 365, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'InitNetwork' not found
I thought there may be some problems with the function's name. So I used Dependency Walker to check the dll functions' names. As expected, the functions' names are strange(the InitNetwork becomes ?InitNetwork##YAHQAD0H#Z):
I searched online and found that there was something wrong during the compiling process when making this DLL.
Here is my question: Is letting the provider recompile the DLL the only way to solve this problem? Or is there another way to let me call these functions in Python?
The DLL seems to provide an interface to C++ functions. That's were the "strange" names come from.
Just recompiling won't help.
... is there another way to let me call these functions in Python?
One could use C++ to write a wrapper DLL, which exposed a C interface.

Weirdness calling str() to convert integer to string in Python 3? [duplicate]

This question already has answers here:
Why does code like `str = str(...)` cause a TypeError, but only the second time?
(20 answers)
Closed last month.
Why is this giving me an error?
>>> variable = str(21)
Traceback (most recent call last):
File "<pyshell#101>", line 1, in <module>
variable = str(21)
TypeError: 'str' object is not callable
That code alone won't give you an error. For example, I just tried this:
~ $ python3.2
>>> variable = str(21)
>>> variable
'21'
Somewhere in your code you're defining that str = something else, masking the builtin definition of str. Remove that and your code will work fine.
Because you've probably overwritten the str function by calling your own variable str.

Categories