-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesson18.py
More file actions
43 lines (38 loc) · 998 Bytes
/
lesson18.py
File metadata and controls
43 lines (38 loc) · 998 Bytes
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
#Built-in
import builtins
print(dir(builtins))
#Global
y = 2
def degree(x):
return y ** x
print(degree(4))
#Local
def degree(x):
y1 = 3
return y1 ** x
print(degree(4))
# print(y1) # It wont work because y1 is local and defines only inside 'degree'
#Enclosing
def degree(x):
y1 = 3
def degree_two():
return y1 ** x
return degree_two()
print(degree(4))
# Function Encapsulation
# Encapsulation is used to hide the values or state of a structured data object inside a class, preventing direct access to them by clients in a way that could expose hidden implementation details or violate state invariance maintained by the methods.
def message(number):
def printMessage():
return 'Number ' + str(number)
return printMessage()
print(message(12))
# print(printMessage()) shows nothing
# Closure (or function closure)
def message(x):
def print_message(y):
return x,y
return print_message
d = message(4)
print(d)
print(d(5))
print(d(7))