Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some built-in Python functions only have pass?

I wanted to see how a math.py function was implemented, but when I opened the file in PyCharm I found that all the functions are empty and there is a simple pass. For example:

def ceil(x): # real signature unknown; restored from __doc__     """     ceil(x)      Return the ceiling of x as a float.     This is the smallest integral value >= x.     """     pass 

I guess it is because the functions being used are actually from the C standard library. How does it work?

like image 470
edgarstack Avatar asked Jul 14 '16 21:07

edgarstack


People also ask

What is the purpose of the pass statement in Python?

The pass statement is used as a placeholder for future code. When the pass statement is executed, nothing happens, but you avoid getting an error when empty code is not allowed. Empty code is not allowed in loops, function definitions, class definitions, or in if statements.

Is pass the same as return None Python?

So pass simply means to do nothing and just go on with processing the next line. Summary: return None is (or can imagined to be) always implicitly added below the last line of every function definition. It can also only appear in functions and immediately exits them, returning a value (or None as default).

How do you skip a block of code in Python?

The break and continue statements in Python are used to skip parts of the current loop or break out of the loop completely. The break statement can be used if you need to break out of a for or while loop and move onto the next section of code.

Which statements are true about built-in functions in Python?

The correct answer is Functions are reusable pieces of programs. Explanation: The correct statement is that Functions are reusable pieces of programs. They allow you to give a name to a block of statements, allowing you to run that block using the specified name anywhere in your program and any number of times.


1 Answers

PyCharm is lying to you. The source code you're looking at is a fake that PyCharm has created. PyCharm knows what functions should be there, and it can guess at their signatures using the function docstrings, but it has no idea what the function bodies should look like.

If you want to see the real source code, you can look at it in the official Github repository in Modules/mathmodule.c. A lot of the functions in there are macro-generated thin wrappers around C functions from math.h, but there's also a bunch of manually-written code to handle things like inconsistent or insufficient standard library implementations, functions with no math.h equivalent, and customization hooks like __ceil__.

like image 75
user2357112 supports Monica Avatar answered Sep 19 '22 23:09

user2357112 supports Monica