Feb-15-2019, 03:59 AM
(Feb-14-2019, 04:19 PM)denis_beurive Wrote: Python recognises "package" as being a package event without the presence of the file "__init__.py".No,from Python 3.3+ supports
Do I misunderstand something ?
Implicit Namespace Packages
that allows to create a package without an __init__.py
file.This however only applies to empty
__init__.py
files.So empty
__init__.py
files are no longer necessary and can be omitted.To give a example,i always have one
__init__.py
that has content to lift sub modules,this to avoid long import statement and make it easier for users of package.
my_pack\ |-- __init__.py color\ |-- base_color.py gradient\ |-- gradient.py
__init__.py
from .color import base_color from .color.gradient import gradient
base_color.py
def red(): return 'rgb(255, 0, 0)'
gradient.py
def hsl_value(): return 'rgba(255, 100, 50, 1.0)'As i have lifted with
__init__.py
there is no need to say color.gradient.hsl_value()
when import.Usage:
λ ptpython >>> from my_pack import base_color, gradient >>> base_color.red() 'rgb(255, 0, 0)' >>> gradient.hsl_value() 'rgba(255, 100, 50, 1.0)'And stop using very old
%s
string formatting.>>> for word in 'f-strings are awesome'.split(): ... print(f'{word.upper():~^20}') ~~~~~F-STRINGS~~~~~~ ~~~~~~~~ARE~~~~~~~~~ ~~~~~~AWESOME~~~~~~~ >>> # f-strings support any Python expressions inside the curly braces >>> name = 'f-string' >>> print(f"My cool string is called {name.upper()}.") My cool string is called F-STRING. >>> a, b = 5, 7 >>> f'{a}/{b} = {a/b:.2}' '5/7 = 0.71'