This question already has answers here:
PyCharm, what is python_stubs?
(2 answers)
Finding the source code for built-in Python functions?
(8 answers)
Closed 1 year ago.
If I go to the implementation of let's say the next() builtin function, I'm forwarded to the builtins.py module to following code:
def next(iterator, default=None): # real signature unknown; restored from __doc__
"""
next(iterator[, default])
Return the next item from the iterator. If default is given and the iterator
is exhausted, it is returned instead of raising StopIteration.
"""
pass
Now, it looks like this functions does nothing but obviously that's not the case.
I understand that this function is implemented in C under the hood, but how and when is this function(or other builtin functions) mapped to the underlying C implementation?
If you have an answer to this question, can you please also provide links that I can read in order to better unterstand this topic?
I'm not asking, where the code is, but how and when the function is mapped to that code
Thank you.
Related
This question already has answers here:
Given a function with closure, can I refer back to it's closure scope?
(1 answer)
What exactly is contained within a obj.__closure__?
(4 answers)
Closed 2 years ago.
I would like to know if there is any method to check whether two functions have the same arguments at runtime in python 3.
Basically, I have this function (func) that takes another function and an argument. I want to check the values assigned to args in the lambda function
func(another_func, args):
return lambda(x : another_func(x, args))
It is not feasible to run the code before and check the results because I am implementing a lazy framework. My main goal is to be able to understand what are the arguments of the function because there is one variable argument that I do not care but there is one static that is created before running the function.
##EDIT
I actually solved this problem using the inspect module (getclosure) for those who are interested!
I actually solved this problem using the inspect module (getclosure) for those who are interested!
Extension (Martijn Pieters):
I think you are referring to getclosurevars(). You can also just access function.closure, and access the value of each cell via its cell_contents attribute.
This question already has answers here:
Get a function argument's default value?
(7 answers)
How to find out the default values of a particular function's argument in another function in Python?
(4 answers)
How can I read a function's signature including default argument values?
(9 answers)
Closed 5 years ago.
I am a python beginner and I currently struggle with some (probably quite simple) stuff. I would like to know the default parameters of a python function, more specifically of cv2.ORB_create().
With the following code I managed to see the 'documentation string':
import cv2
orb = cv2.ORB_create()
print(cv2.ORB_create.__doc__)
However, print(cv2.ORB_create.__defaults__) just gives me an error;
AttributeError: 'builtin_function_or_method' object has no attribute '__defaults__'
Maybe I am missing a link between functions, modules, etc. but I am really stuck...
Since the concern was brought up that this is a duplicate. I also tried inspect.signature and by extension inspect.getargspec, but this give me another error ValueError: no signature found for builtin <built-in function ORB_create>.
cv2.ORB_create() seems to be a function written with the Python extensions (In C instead of Python). As such, it isn't a "normal" function object, and there is no way to see the default values reliably (As it is handled manually in C).
One possible solution would be to look at the __text_signature__ attribute, but this may not be reliable. See What are __signature__ and __text_signature__ used for in Python 3.4.
This question already has answers here:
Why does Python code use len() function instead of a length method?
(7 answers)
Closed 8 years ago.
The "intuitive" way of getting an iterator for someone who usually programs in Java, C++, etc is something like list.iterator().
Why did the Python folks choose to have it as a general function like len() (which results in iter(list) rather than list.iter())?
The same question can be asked for the length of a construct as well (len()).
iter() supports different types of objects.
You can pass in either a sequence (supporting length and item access) or an iterable (which produces an iterator by calling obj.__iter__()) or an iterator (which returns self from __iter__).
The Java list.iter() then is served by list.__iter__() in Python, but the iter() function allows for more types. You can customise the behaviour with a __iter__ method but if you implemented a sequence instead, things will still work.
There is also a second form of the function where a callable and a sentinel are passed in:
iter(fileobj.readline, '')
iterates over a file object by calling the readline() method until it returns an empty string (equal to the second argument, the sentinel).
Then there is the Principle of Least Astonishment argument; iter() gives the standard library a stable API call to standardise on, just like operators do; no need to look up the documentation of the class to see if it implemented obj.iter() or obj.iterator() or obj.get_iterator().
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
python 'self' explained
I am a beginner in Python. I was going through the tutorials on Classes and Iterators when I had a doubt that I was unable to explain to myself. The program text below was a part of a class which calculates area.
def __init__(self,len,wid):
self.length=len
self.width=wid
def calculate_area(self)
return self.length*self.width
def print_area(self)
print 'Area='+str(self.calculate_area())
What I am unable to understand is why do the function's argument list have "self"? What is its role? Why are every variable resolved with "self"?
This is similar to this pointer in C++ (if you have come from C++ background)
Typical usage would be that the members of objects can be referenced by self in case if there is an ambiguity. e.g.
def calculate_area(self, length)
return self.length*self.width
Above length is an argument for calculate_area function.
if the object also has length member then it can be resolved by using self.length
Refer existing answer here:
What is the purpose of self?
I really don't know I'm too new on the Python world but I think that Python does not provide the this value as C# or Java do, so this is the mechanism that Python use to define itself in its classes.
Anyway you can see that you don't need to pass the self as parameter in the function call, because Python does for you.
This is my theory, but I'm also interested to know it so If anyone can say more about this, I think we will be very thankfull.
See you!
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why does defining getitem on a class make it iterable in python?
I have a class that is basically a wrapper for a python list. Within this class I have defined __getitem__, __setitem__, and __len__ . I have not defined __iter__ for this class.
when I go:
thing = ListWrapper(range(4))
for i in thing :
print i
I get the output:
0
1
2
3
Which is nice, but I expected an error message of some sort saying that python could not find an iterator. I've given the documentation a look and can't find anything referencing default iterators. Furthermore, tracing through the code in PyDev shows that it is calling the __getitem__ method each iteration.
I was wondering if it is good practice to depend on this behavior in my code. It doesn't fell quite right to me at this point. Does Python guarantee that classes with __getitem__ and __len__ will be treated as if they have a defined iterator? Any other information on weirdness this may cause is also welcome.
If a class doesn't have __iter__, but does have __getitem__, the iteration machinery will call it with consecutive integers until it runs out.