Can we implement source()
function in Python?
Python has import
statement but the source is executed in the scope of the module.
R's source()
reads a script and just runs it in the global scope.
I have a candidate.
def source(filename, code=False):
print(f'* source("{filename}")')
if not (filename and os.path.isfile(filename)):
print('! invalid filename or file does not exist')
return None
else:
with open(filename, encoding = 'UTF-8') as f:
lns = f.read()
if code:
print(lns)
exec(lns, globals(), globals())
return True
Can you think of any side effects or negative result from this?