This question already has answers here:
Python: get only classes defined in imported module with dir()?
(3 answers)
In Python, how do I get the list of classes defined within a particular file?
(4 answers)
Closed 2 years ago.
How do I get all the classes in a module? For example if a module has classes A, B and C, how do I get the names of all these classes through code?
Try this:
import importlib, inspect
inspect.getmembers(
importlib.import_module("your_module"),
inspect.isclass
)
this will give you what you are looking for.
Related
This question already has answers here:
Why is globals() a function in Python?
(1 answer)
Reason for globals() in Python?
(9 answers)
Closed 2 days ago.
I'm tracing a legacy project, and see the source code is doing this:
globals()[f'{object_name}s']
I know we use global keyword when we want to change the value for the global variable in a local scope.
But what does global() do?
You can use the python interperter or pydoc to help you answer questions about keywords
$> pydoc globals
output:
Help on built-in function globals in module __builtin__:
globals(...)
globals() -> dictionary
Return the dictionary containing the current scope's global variables.
(END)
This question already has answers here:
Using global variables between files?
(9 answers)
Closed 2 years ago.
I would like to know please, how can I define variables in a python file and share these variables with their values with multiple python files?
To do this, you can create a new module specifically for storing all the global variables your application might need. For this you can create a function that will initialize any of these globals with a default value, you only need to call this function once from your main class, then you can import the globals file from any other class and use those globals as needed.
You can create a python module
Create a py file inside that module define variables and import that module in the required places.
This question already has answers here:
import module from string variable
(7 answers)
How can I import a module dynamically given its name as string?
(10 answers)
Closed 5 years ago.
I have a list of related function names which I want to iterate thru, calling the function held in the variable. But no matter how I try it, I get: "TypeError: 'TweetTokenizer' object is not callable"
Following the solution in Calling a function of a module from a string with the function's name in Python (of which it is suggested this question is a duplicate), finds the functions in the nltk module and assigns them. But my resulting "tok_alg" function is still failing as not callable. Any advice on why this is happening would be appreciated.
#!/usr/bin/env python
import nltk
import os
testTxt="I'll have a Bloomin' Onion."
Tokenizers = [ 'Tweet', 'MWE', 'TreebankWord' ]
for tokenizer in Tokenizers:
tokz = tokenizer + 'Tokenizer'
tok_alg = getattr(nltk, tokz)()
result = tok_alg(testTxt)
print(result)
Listing the functions does work, viz
for tokenizer in [ TweetTokenizer(), MWETokenizer() ]:
result = tokenizer.tokenize(testTxt)
But the suggested conversion of str variables via getattr() is not working for NLTK. While this is elegant and practical, I need the string variables for other purposes. Surely there is some way to vivify these into a function call. What am I missing?
This question already has answers here:
Python function pointer
(8 answers)
Closed 9 years ago.
if I have a string like 'module.function', How can I execute function just by one step?
likesomefunction('os.error','args')
You can dynamically get the modules using sys.modules and then you can use getattr to get the attributes from the module, like this
import sys
func = "os.error"
module, function = func.split(".", 1)
getattr(sys.modules[module], function)()
sys.modules can give only the modules which are already loaded. So, if you want to load a module dynamically you can use __import__ function like this
For example,
module, function = "math.factorial".split(".", 1)
print getattr(__import__(module), function)(5)
Output
120
All you need to do is
from module import function
and you'll be able to call
function(x, y, z)
in your code.
This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 8 years ago.
I need to create a variable in a different script from the main one in my game I am working on, with Python and Pygame.
For example:
def test():
a = 10
def testing():
return a
Then I run code like this:
import (script name)
script name.test()
script name.testing()
And after this, it gives an error. How can I fix this problem?
'a' in testing() is not a global variable and hence it's not recognised from previous function test(). If you really want to use 'a' from test() then you can probably define 'a' as Global Variable.