In Python, is it always advisable to access import
-ed variables without going through the parameters of a function? Doesn't this violate the black-box paradigm?
For instance: given the statement from collections import deque
, I wouldn't expect that one would instantiate a deque
object to be passed as a parameter along with every function. Instead, I would expect that a deque
would be instantiated as needed.
Suppose, though, that the imported object didn't belong to the canonical libraries in Python. Would it be preferred to access such an object through the parameters of a function, or through the global scope?
Edit: To help illustrate what I mean, take for instance the code below:
from collections import deque
def my_func():
# this seems to be OK
nodes = deque()
On the other hand, suppose that we had some other kind of object. Would this be encouraged in Python?
from my_module import SomeClass
def my_func():
# SomeClass accessed through global scope
instance_of_some_class = SomeClass()
Doesn't the above violate black-box coding? Alternatively:
from my_module import SomeClass
def my_func(some_class):
# SomeClass accessed through local scope
some_class.do_a_thing()
def main():
# I suppose SomeClass() is being accessed globally here...but this is the crux of my question nonetheless.
instance_of_some_class = SomeClass()
my_func(instance_of_some_class)
I realize that as a matter of design, this may be open to opinion; mainly, I was curious if there is a prescribed recommendation in Python.
Aucun commentaire:
Enregistrer un commentaire