In C++ I would write:
namespace LowerLevel{
int DoStuff() {}; //
}
And access it via:
LowerLevel::DoStuff()
How do I do both in Python?
In C++ I would write:
namespace LowerLevel{
int DoStuff() {}; //
}
And access it via:
LowerLevel::DoStuff()
How do I do both in Python?
The closest in spirit would be to use a module.
In a file lower_level.py
you'd have
def do_stuff():
pass
and then elsewhere you'd do:
import lower_level
lower_level.do_stuff()
EDIT / addendums: That's the simple way. There's things like packages, where you'd have a folder lower_level
, and inside that folder is an __init__.py
identifying lower_level
as a package. Inside that package you'd have .py
files that'd be modules, or you put certain imports and declarations into the __init__.py
. But it can also just remain empty. I guess packages would amount to nested namespaces.
- prog.py
\MyNameSpaceA
- __init__.py # just an empty file
- ObjA.py # A python Module
\SubPackageB
- __init__.py
- ObjB.py # another python module
One more option is to define everything in a function and return the locals. The advantage is that all definitions are in the same scope, so they can reference each other without any prefixes. Also you can have many of these in the same file.
import argparse
def InitLowerLevel():
def DoStuff():
return 1
def DoOtherStuff():
return DoStuff()
kFoo = 0
return argparse.Namespace(**locals())
lower_level = InitLowerLevel()
lower_level.DoStuff()
The argparse
module may also be an option to consider, depending on your use case. The original answer is what I would say the better Software Development best practice, but this module provides the functionality you originally asked for.
It allows the dot notation to set and get variables. But complains less than dicts for when you are trying to add new variables/functions.
Python 3
>>> import argparse
>>> def my_test_function(): return
>>> ...
>>> args = argparse.Namespace()
Namespace()
>>> args.minutes = 5
>>> args.test = my_test_function
>>> args
Namespace(minutes=5, test=<function times_close_enough at 0x000001D2BEEAA550>)
Although the proper way to use argparse
can be found at its documentation
, some further examples can be found on this StackOverflow question (helps if you understand better by example!)