rRdfined a built-in function [duplicate] - python

This question already has answers here:
How to restore a builtin that I overwrote by accident?
(3 answers)
Closed 3 years ago.
Redefined builtin function len() in spyder like len = 100;
Now when I am trying to find the length of the string it says "'int' object is not callable"
How to undo this change or correct this?

Since len is an inbuilt python function, you should avoid using that as a variable. try prefixing your 'len' with '_' and use '_len = 10' instead of just 'len'.

Related

What does the python interactive console output? [duplicate]

This question already has answers here:
Why does typing a variable (or expression) print the value to stdout?
(2 answers)
Closed 3 years ago.
When I import the datetime module for example, and create a datetime.datetime object, thus that this datetime.datetime object is the value of the expression, it returns
datetime.datetime(*values inserted*)
But this is not the _str__() return value of datetime.datetime.
How is the output of the interactive console defined to behave?
Is there a specification or comprehensible explanation?
It shows the value of __repr__().

if by mistake a user overwrote a keyword, for example, range [duplicate]

This question already has answers here:
How to restore a builtin that I overwrote by accident?
(3 answers)
Closed 4 years ago.
for range in ranges:
print(range)
I have used the above code by mistake overwrote range keyword - Is there any way to retrieve this keyword?
You can get it from the builtins module:
import builtins
range = builtins.range
Or if you're using Python 2.7 or earlier:
import __builtin__
range = __builtin__.range
Issue a
del range
and you'll have your range back.

I have error when I use exec() to call functions in python 3 [duplicate]

This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Use a string to call function in Python [duplicate]
(3 answers)
Closed 4 years ago.
I have this simple function:
def fu():
return "great"
I need to call it by using a string,
So I tried this:
print(exec("fu()"))
But the the output I got was:
None
How do i fix it?
As in comments says you can not use exec for this purpose.
but eval will do what you want, full doc here:
>> eval('fu()')
"great"
Note that using eval is not the best practice.
There is a better way to access this function with globals or locals based on where you define your function, and I think it's better to use this instead of eval:
>> globals()['fu']()
"great"

How can i convert a string in a python method? [duplicate]

This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Closed 8 years ago.
I receive a string, for example "save". And i have a method save with paramethers.
How can i convert the string save in a call to save().
I tried with eval and exec.
Edit: Solved here --> Calling a function of a module from a string with the function's name in Python
def some_method(self):
save_method = getattr(self, 'save')
save_method() # save()

Join two strings into a callable string 'moduleA' + 'func1' into moduleA.func1() [duplicate]

This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Closed 9 years ago.
I got this:
tu = ("func1", "func2", "func3")
And with the operation I am looking for I would get this for the first string:
moduleA.func1()
I know how to concatenate strings, but is there a way to join into a callable string?
getattr(moduleA, 'func1')() == moduleA.func1()
You should use getattr builtin function. Try:
getattr(moduleA, 'func1')()
If you mean get a function or method on a class or module, all entities (including classes, modules, functions, and methods) are objects, so you can do a func = getattr(thing 'func1') to get the function, then func() to call it.

Categories