Suppose I use some 3rd party module that depends on code from another one:
# third_party.py
from package import fun, A
class B(A):
def foo(self):
self.do()
self.some()
self.stuff()
return fun(self)
And then I want to inherit this class in my code to change functionality:
# my_code.py
from third_party import B
# from third_party import fun?
# from package import fun?
class C(B):
def foo(self):
return fun(self)
What is better: from package import fun or from third_party import fun to get access to fun?
I like second variant since I may do not bother with actual paths and import all dependences from third_party package, but has this way any drawbacks? Is this a good or bad practice?
Thanks!
I dont’t think that importing a function/class from a third party package is a bad practice, it may even has some benefits (e.g: If you want to monkey patch a package, or need to be sure, that something is set up correctly).
It even may be necessary to support a variety of setups. Consider the
ElementTreeAPI, which is differently available on specific Python versions and may even be provided from third party libraries (taken from here):Now, it is guranteed that
somepackagecontains a workingetreeimplementation, even on different Python installations and your package serves as an abstraction.