Created
May 1, 2020 15:11
-
-
Save zekefarwell/f4d25de62e0ff8f9d8bf3becab1a948d to your computer and use it in GitHub Desktop.
Python Import Test Cases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
import thing as t | |
class Logger: | |
# This type hint would throw a circular error because | |
# without the __future__ import above. | |
# That import defers the execution of the typehint until after | |
# thing is fully initialized | |
def log(self, two: t.Two): | |
print(f'{two.name}: {two.get_info()}') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# No circular error because full module imported | |
# instead of a class or function within | |
import thing | |
# This would be an error because we attempt to | |
# access One before the thing module is fully initialized | |
# from thing import One | |
class Printer: | |
def print(self): | |
# thing module is fully initialized by the time we use | |
# the One class within thing so no issue here | |
one = thing.One() | |
print(f'{one.name}: {one.get_info()}') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import thing | |
thing_one = thing.One() | |
thing_one.print() | |
thing_two = thing.Two() | |
thing_two.log() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logger | |
import printer | |
class One: | |
name = "Thing One" | |
def print(self): | |
printer.Printer().print() | |
def get_info(self): | |
return "The first thing" | |
class Two: | |
name = "Thing Two" | |
def log(self): | |
logger.Logger().log(self) | |
def get_info(self): | |
return "The second thing" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment