LoginSignup
3
0

More than 5 years have passed since last update.

__init__.pyをエイリアスっぽく使う

Last updated at Posted at 2018-04-11
lib
 ┗__init__.py
main.py

こんなディレクトリ構造があったときに

main.py

import lib

lib.foo()

みたいに呼びたいときは、通常、次のようにlib/__init__.pyにこんな感じで関数を付け足す。

__init__.py
def foo():
  print('called foo')

でも、__init__.pyはあまり汚したくない。そういった場合は別のモジュールを用意してimportすれば、main.pyから同じように呼び出せる。

lib
 ┗__init__.py
 ┗funcs.py
main.py
__init__.py
from .funcs import foo
funcs.py
def foo():
  print('called foo')

気づいてしまえば何のことはない、すごく簡単。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0