Conveniently import several classes from modules in a Python package -
i'm creating framework syntax tree. have folder/package syntax contains 1 syntax tree element class in each file, structure looks like:
syntax/list.py -> contains class list syntax/block.py -> contains class block syntax/statement.py -> contains class statement now i'd import folder source file in way can access classes like
block = syntax.block() is possible @ all? far, ended need syntax.block.block() not nice...
project structure
syntax ├── block.py ├── __init__.py the class
# syntax/block.py (this file ought lowercase, leave pascalcase class names) class block(object): pass imports in __init__
# syntax/__init__.py .block import block # relative import . using package
in [5]: import syntax in [6]: b = syntax.block() in [7]: b out[7]: <syntax.block.block @ 0x108dcb990> alternative if open re-organization
unlike languages require put single class file same name (class block inside file block.py), python pretty flexible this.
you can put many classes in syntax.py , import syntax alone, access syntax.block (no imports in __init__.py required this)
# syntax.py class block(object): pass class list(object): pass then can use follows
import syntax b = syntax.block() l = syntax.list()
Comments
Post a Comment