2023-06-14 15:50:08 -07:00
|
|
|
# import sys
|
|
|
|
|
# # Python 3
|
|
|
|
|
# print(sys.maxsize) # Output: 9223372036854775807 (on most platforms)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# list1 = [1, 2, 3, 4, 6]
|
|
|
|
|
# list2 = [2, 4, 6, 7, 8]
|
|
|
|
|
# # Find common values
|
|
|
|
|
# common_values = []
|
|
|
|
|
# for item in list1:
|
|
|
|
|
# if item in list2:
|
|
|
|
|
# common_values.append(item)
|
|
|
|
|
|
|
|
|
|
# # Find different values
|
|
|
|
|
# different_values = []
|
|
|
|
|
# for item in list1:
|
|
|
|
|
# if item not in list2:
|
|
|
|
|
# different_values.append(item)
|
|
|
|
|
# for item in list2:
|
|
|
|
|
# if item not in list1:
|
|
|
|
|
# different_values.append(item)
|
|
|
|
|
|
|
|
|
|
# print("Common values:", common_values) # Common values: [2, 4, 6]
|
|
|
|
|
# print("Different values:", different_values) # Different values: [1, 3, 7, 8]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # Find common values
|
|
|
|
|
# common_values = set(list1) & set(list2)
|
|
|
|
|
|
|
|
|
|
# # Find different values
|
|
|
|
|
# different_values = set(list1) ^ set(list2)
|
|
|
|
|
|
|
|
|
|
# print("Common values:", common_values)
|
|
|
|
|
# print("Different values:", different_values)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# def outer_function():
|
|
|
|
|
# outer_variable = 10
|
|
|
|
|
|
|
|
|
|
# def inner_function():
|
|
|
|
|
# inner_variable = 20
|
|
|
|
|
# print("Inner function: ", inner_variable)
|
|
|
|
|
# print("Outer function (captured variable): ", outer_variable)
|
|
|
|
|
|
|
|
|
|
# return inner_function
|
|
|
|
|
|
|
|
|
|
# # Create a closure by calling the outer function
|
|
|
|
|
# closure = outer_function()
|
|
|
|
|
|
|
|
|
|
# # Access the cell object from the closure
|
|
|
|
|
# cell_object = closure.__closure__[0]
|
|
|
|
|
|
|
|
|
|
# # Access and print the value stored in the cell
|
|
|
|
|
# print("Cell contents: ", cell_object.cell_contents)
|
|
|
|
|
|
|
|
|
|
# # Set a new value to the cell
|
|
|
|
|
# cell_object.cell_contents = 30
|
|
|
|
|
|
|
|
|
|
# # Access and print the updated value from the closure
|
|
|
|
|
# print("Updated cell contents: ", closure.__closure__[0].cell_contents)
|
|
|
|
|
|
|
|
|
|
|
2023-06-14 15:54:30 -07:00
|
|
|
|
2023-06-14 15:50:08 -07:00
|
|
|
def greet(name):
|
|
|
|
|
message = f"Hello, {name}!"
|
|
|
|
|
print(message)
|
|
|
|
|
|
|
|
|
|
# Accessing code object
|
|
|
|
|
code_obj = greet.__code__
|
|
|
|
|
print(code_obj.co_name) # Output: greet
|
|
|
|
|
print(code_obj.co_argcount) # Output: 1 (number of arguments)
|
|
|
|
|
print(code_obj.co_varnames) # Output: ('name', 'message')
|
|
|
|
|
|
|
|
|
|
# Accessing function object
|
|
|
|
|
print(greet.__name__) # Output: greet
|
|
|
|
|
print(greet.__defaults__) # Output: None (no default arguments)
|
|
|
|
|
print(greet.__globals__) # Output: {'__name__': '__main__', ...} (global namespace)
|
|
|
|
|
|
|
|
|
|
# Modifying function object attributes
|
|
|
|
|
greet.__name__ = 'say_hello'
|
|
|
|
|
greet.__defaults__ = ('John',)
|
|
|
|
|
greet.__globals__['message'] = 'Welcome!'
|
|
|
|
|
|
|
|
|
|
# Call the modified function
|
|
|
|
|
greet() # Output: Hello, John!
|