How to get all class, variables, func names in current namescope? [duplicate] - python

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

Related

How do you assign local variables within a function using exec? [duplicate]

This question already has answers here:
How to get local variables updated, when using the `exec` call?
(3 answers)
Setting variables with exec inside a function
(2 answers)
Closed 3 years ago.
I have a function with an unspecified number of input strings which are intended to be variable names. For example
def f(*args):
for arg in args:
exec('{} = 1'.format(arg))
return a
f('a', 'b')
When running the code, I recieve the following error
NameError: name 'a' is not defined
How do I assign local variables for the function which are to be manipulated or returned? The solution provided in this similar but different question requires creating the variables outside the function, i.e. adding them to the global namespace, but that is not what I want.

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"

get variable name of instance [duplicate]

This question already has answers here:
Simpler way to create dictionary of separate variables?
(27 answers)
How can you print a variable name in python? [duplicate]
(8 answers)
Closed 8 years ago.
Can a Python instance get the name of the variable which is used to access the object?
This example code shows what I need:
foo=MyClass()
foo.get_name() --> 'foo'
bar=foo
bar.get_name() --> 'bar'
I know that this is black magic and not clean code. I just want to know if it is possible.
I know that bar.__name__ returns the name, but I need it inside an own method.
How can get_name() be implemented?
This is not a duplicate of questions which answer is __name__

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