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__().
Related
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'.
This question already has an answer here:
What's the difference between globals(), locals(), and vars()?
(1 answer)
Closed 4 years ago.
How can I get all class, object, function names in current namescope in Python? How can I differentiate between them(e.g. get only class names)?
Use help() or dir().
You can dump the output into a list of strings and read from there too to differentiate what you require
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.
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"
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()