Python - check for class existance

♀尐吖头ヾ 提交于 2021-01-21 09:14:53

问题


Is there a way to check if a class has been defined/exists? I have different options on a menu, and some of them require inherited variables, so if you try to select that function before you have set the variables, it crashes. My class is called Gen0, and I started it by simply putting

class Gen0():

Followed by the rest of the code to set the variables. For context, I am doing a population model, so the initial values need to be set (option 1) before displaying (option 2), using (option 3) or exporting (option 4) them.


回答1:


Your situation is not entirely clear, so I'm just going to answer the question "Is there a way to check if a class has been defined/exists?"

Yes, there are ways to check if a class has been defined in the current scope. I'll go through a few.

1. It's Better to Ask Forgiveness Than Permission

Just try to use it!

try:
    var = MyClass()
except NameError:
    # name 'MyClass' is not defined
    ...

This is probably the most Pythonic method (aside from just being sure you have the class imported/defined).

2. Look Through Current Scope

Everything in the current scope is given by dir(). Of course, this won't handle things that are imported! (Unless they are imported directly.)

if not 'MyClass' in dir():
    # your class is not defined in the current scope
    ...

3. Inspect a Module's Contents

Perhaps you want to see if the class is defined within a specific module, my_module. So:

import my_module
import inspect

if not 'MyClass' in inspect.getmembers(my_module):
    # 'MyClass' does not exist in 'my_module'
    ...

Now, it's worth pointing out that if at any point you are using these sorts of things in production code, you're probably writing your code poorly. You should always know which classes are in scope at any given point. After all, that's why we have import statements and definitions. I'd recommend you clean up your question with more example code to receive better responses.




回答2:


Instead of checking whether the variable exists, you might want to go for a EAFP solution. For example:

try:
   print(var)
except NameError:
   var = 2
   print(var)

If var is not defined before this piece of code, it will assign it to 2 and print it.




回答3:


I'm guessing gen0 is defined something like this:

class gen0:
    pass

as opposed to initialising the attributes in your class on object creation

class gen0:
    def __init__(self):
        # this function will be run when you call "g = gen0()"
        # in the main body of you code. and the following properties 
        # will be set on object "g"
        self.juvenile_population = 0
        self.juv.... etc

so I'm also guessing that you are getting an AttributeError when you try to access something like juvenile_population before it is set.

Initialising your classes properly is the best way to do object orientated programming IMHO.

However the literal answer to your question would be:

 g = gen0()
 if "juvenile_population" in dir(g):
      .... etc

Note this checks only that the object has the juvenile_population attribute, you would need a lot more of these statements to check every attribute, similarly you could use the EAFP approach in Douglas' answer but again that's going to bulk out your code a lot with try statements.




回答4:


Recently I've run into the same question writing my custom protocol. This is my solution, no extra modules, no evals used:

import handlers

# ... skip ...
# create example request
request = {'message': 'Test', 'param1': True, 'param2': False}

Handler = getattr(handlers, '__dict__', {}).get(request.get('message'))
if not Handler:
  raise InvalidRequestException('Invalid request')

handler = Handler(request)
# process request...

# ... skip ...

It doesn't distinguish classes/functions – you should make an additional check.




回答5:


In python 3.8 you can do:

class_name = "MyClass"
is_class_in_scope = globals().get(class_name) is not None

Example:

>>> from django.test import TestCase
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'TestCase': <class 'django.test.testcases.TestCase'>}
>>> globals().get('TestCase') is not None
True


来源:https://stackoverflow.com/questions/38171243/python-check-for-class-existance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!