built-in

subclassing the builtin enumerate in python

subclassing the builtin enumerate in python Question: Consider the code below. I am trying to subclass the builtin enumerate so that it prints a line for every turn of the for loop. The code seems to be working, which is surprising, because I have never called super().__init__(x). So, what is happening here? Who is initializing …

Total answers: 1

add more than two objects with __add__ function

add more than two objects with __add__ function Question: I have a class it can successfully add two variables of object of class a class a(): def __add__(self, other): return self.val+other.val def __init__(self,a): self.val=a aa=a(22) bb=a(11) aa+bb 33 But when I try to give it third object to add, it through error cc=a(11) aa+bb+cc Traceback …

Total answers: 3

Is there a way to restore a builtin function after deleting it?

Is there a way to restore a builtin function after deleting it? Question: After deleting a builtin function like this, I want to restore it without restarting the interpreter. >>> import builtins >>> del builtins.eval >>> builtins.eval = None I tried reloading the builtin module using importlib, that didn’t restore eval. >>> import importlib >>> …

Total answers: 2

Implementation of 'print' in Python

Implementation of 'print' in Python Question: I was just curious as to how built-in function ‘print’ might be working behind the scenes in Python3. So, the following code snippet is my attempt to write a print function of my own but I’m not sure if it accurately represents how the actual ‘print’ works: import os …

Total answers: 1

What does the name of the ord() function stand for?

What does the name of the ord() function stand for? Question: The official Python documentation explains ord(c) ord(c): Given a string representing one Unicode character, return an integer representing the Unicode code point of that character. For example, ord(‘a’) returns the integer 97 and ord(‘€’) (Euro sign) returns 8364. This is the inverse of chr(). …

Total answers: 2

No module named builtins

No module named builtins Question: I’m trying to convert my .py script into an executable using py2exe. I’ve had a number of issues so far that have been largely addressed by the “options” in the setup file below. But now I have a problem that I have not been able to find a solution for, …

Total answers: 4

Python built-in function "compile". What is it used for?

Python built-in function "compile". What is it used for? Question: I came across a built-in function compile today. Though i read the documentation but still do not understand it’s usage or where it is applicable. Please can anyone explain with example the use of this function. I will really appreciate examples. From the documentation, the …

Total answers: 3

Exponentials in python: x**y vs math.pow(x, y)

Exponentials in python: x**y vs math.pow(x, y) Question: Which one is more efficient using math.pow or the ** operator? When should I use one over the other? So far I know that x**y can return an int or a float if you use a decimal the function pow will return a float import math print( …

Total answers: 7