-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicFunctions.py
More file actions
59 lines (46 loc) · 1.31 KB
/
DynamicFunctions.py
File metadata and controls
59 lines (46 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# very powerful, very easy to misuse
def EVAL():
x = 1
expression = "x * 3 + x**2"
result = eval(expression)
assert result == 4
def EXEC():
code = """
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
"""
exec(code) # doesn't return anything
def COMPILE(): # if you're executing something many times you might want to pre-compile it
code = 'print("Hello, World!")'
compiledCode = compile(code, "<string>", "exec")
exec(code)
def GLOBAL():
global var
var = 42
globalVars = globals()
assert globalVars["var"] == 42
globalVars["var"] = 43
assert globalVars["var"] == 43
def LOCAL():
localVar = 10
localVars = locals()
assert localVars["var"] == 10
# DONT MODIFY LOCALS
def VARS():
class MyClass:
def __init__(self):
self.attribute = 543
obj = MyClass()
obj.attribute2 = "hello"
varsDict = vars(obj)
assert varsDict["attribute"] == 543
assert varsDict["attribute2"] == "hello"
def IMPORT(): # really dangerous, DONT USE UNLESS REALLY NECESSERY
math = __import__("math")
assert math.sqrt(16) == 4
math2 = __import__("math")
assert math is math2
import importlib # a much prefered and user friendly way to do imports
math3 = importlib.import_module("math")
assert math3 is math is math2