This question already has answers here:
Finding the source code for built-in Python functions?
(8 answers)
Closed 3 years ago.
Where can I find the source code for the implementation of object.__new__. It shows as "built-in". I would like to see how it works.
>>> object.__new__
<built-in method __new__ of type object at 0x822060>
Python3's object.__new__ is internal (built-in). It's not written in Python. You can find the C code for it as object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) in typeobject.c.
Related
This question already has answers here:
Is it possible to access original function which has been overwritten in python [duplicate]
(3 answers)
Closed 4 months ago.
I'm doing maintenance on a huge file that follows like this
...stuff
def open(account):
...do stuff
...stuff
However I need to write a str to a txt, so I was gonna do the following
with open(filename,"a") as file:
file.write(mystr)
and this is calling the open(account) method, which is being used in a lot of classes, so renaming is not an option, how can I call the open method from the built-in functions from python and not the one with the same name?
You can do __builtins__.open().
This question already has answers here:
What do ellipsis [...] mean in a list?
(5 answers)
Closed 2 years ago.
I've run into this unseen list object, when I tried to play with list append, and I've searched hard but unable to find much information. So this is what's happening:
L = ['dinosaur']
L.append(('theropoda', L))
print(L)
# ['dinosaur', ('theropoda', [...])]
Questions - what's the [...] means here? Thanks.
As mentioned in the comments, Python will not attempt to include a circular/recursive reference in the representation of a list.
It would appear that the __repr__ function (also used by lists to create the string for printing) is implemented via reprlib with recursive support. Without it, you would end up with a RecursionError as to output the list, Python must include the nested version of the list, which also needs the nested version, and so on. Instead, it outputs the special value of ... which indicates to you that it is a recursive reference.
This question already has answers here:
How to import everything exported from a file with ES2015 syntax? Is there a wildcard?
(5 answers)
Closed 4 years ago.
Is there anyway not to write this:
import {replace_in_mtstr, tot_width, rationalize, eval_expression, ascii_to_latex, latex_to_ascii, getIndicesOf, cleanIndices, change_sign, exponentiate, multiply_grouped_nodes, flip_fraction, add_brackets, are_of_type, any_of_type, have_same_ancestors, have_same_type, have_same_text, have_single_factor, have_same_denom, are_same_terms, get_prev, get_next, get_all_next, has_op, parse_mtstr, parse_poly, prepare} from "./functions";
in ES6? These are all the exported functions in a different file. The closes thing I know is import * as object from 'file';. However, I want to have the functions available directly, not inside an object. Is this possible in ES6?
As the comments indicate, this is not possible.
This question already has an answer here:
Nested arguments not compiling
(1 answer)
Closed 7 years ago.
I have a piece of code that looks completely fine,
def _change_id(self, model, path, it,(old_id, new_id)):
But whenever I try to run it in my terminal python returns, "SyntaxError: invalid syntax"
The use of the tuple parameter was removed in python 3.0. This caused more issues than it was worth. You can rewrite it this way:
def fun(p1, b_c, p2):
b, c = b_c
the parameter b_c was a tuple:
fun(1, (1, 2), 3)
Its called Removal of Tuple Parameter Unpacking (only in python3)
see http://legacy.python.org/dev/peps/pep-3113/
Apparently, tupple parameter unpacking was removed in python 3 as per this link here
Edit: #Yoav and #jonrsharpe beat me to it
This question already has answers here:
Get the type of a variable in VBScript
(6 answers)
Closed 7 years ago.
I am passing one object to function which needs to take action according to object type. e.g. If object is of "Scripting.Dictionary" then count keys and if it is "Scripting.FileSystemObject" then close it.
In short as like typeof in c# and variableName.class in java, how we can find which scripting object we are using ?
Use the TypeName function to get the type of a variable.
TypeName Function
Returns a string that provides Variant subtype information about a variable.
TypeName(varname)