Let's say I have a class defined in a file:
import stuff
import more stuff
import stuff that takes a long time to import
class Foo(object):
def __init__(self, arg1, arg2 etc.):
self.arg1 = arg1
self.arg2 = arg2
# Lots of other stuff
# Lots of methods
And in another file I have this code:
from big_file import Foo
def do_stuff(obj):
if isinstance(obj, Foo):
do_stuff
else:
do_other_stuff
Let's say the file Foo takes a long time to import for reasons out of my control. How can I refactor this code to not import Foo but still reliably check the type? I don't think duck typing is appropriate for my particular case.
Should I e.g. check the string representations of the bases of the obj
? Or is there another, more canonical way?
Example 2: Example of type() with a name, bases, and dict Parameter. If you need to check the type of an object, it is recommended to use the Python isinstance() function instead. It's because isinstance() function also checks if the given object is an instance of the subclass.
Python isinstance() Function The isinstance() function returns True if the specified object is of the specified type, otherwise False . If the type parameter is a tuple, this function will return True if the object is one of the types in the tuple.
Using isinstance() function, we can test whether an object/variable is an instance of the specified type or class such as int or list. In the case of inheritance, we can checks if the specified class is the parent class of an object. For example, isinstance(x, int) to check if x is an instance of a class int .
Check pandas Version from Command or Shell mode From a command line or shell run the pip list command to check the pandas version or get the list of the package installed with the currently installed version next to the package. If you don't have pandas installed then this command doesn't list it.
Typically, this would be a non-issue. If you have an instance of big_file.Foo
, then its parent module has already been imported earlier on even if not explicitly referenced from your other file. Python modules are only loaded once on the first import (provided you're not doing any explicit reloading or messing with sys.modules
). Since it's already been imported, doing an import big_file
in your other file should run instantaneously.
However, if your other file will only encounter a big_file.Foo
under certain circumstances and big_file
is only imported elsewhere when it's actually need, then you could inspect the object's class as such (this does not support subclasses):
def do_stuff(obj):
if (obj.__class__.__module__, obj.__class__.__name__) == ('big_file', 'Foo'):
do_stuff
else:
do_other_stuff
Since you've indicated that big_file.Foo
could be imported at any point in the application and you want to support subclasses, you could check to see if its module has been imported and conditionally check the type.
import sys
def is_Foo(obj):
if 'big_file' in sys.modules:
return isinstance(obj, sys.modules['big_file'].Foo)
else:
return False
def do_stuff(obj):
if is_Foo(obj):
do_stuff
else:
do_other_stuff
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With