-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOPS.py
More file actions
57 lines (42 loc) · 1.31 KB
/
OOPS.py
File metadata and controls
57 lines (42 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
# Classes and Objects in Python
class buisness:
def employee(x):
x.name = input("Enter the name of the employee: ")
x.age = int(input("Enter the age of the employee: "))
x.salary = int(input("Enter the salary of the employee: "))
buisness = buisness()
print(buisness.employee())
print("Name of the employee: ", buisness.name)
# Constructor in Python
class Person:
def __init__(self, name, age): # Constructor
self.name = name # Initialize attributes
self.age = age
# Creating an object
p1 = Person("Alice", 25)
print(p1.name) # Output: Alice
print(p1.age) # Output: 25
# Decorators in Python
def decorator(func):
def wrapper():
print("Before the function call")
func()
print("After the function call")
return wrapper
@decorator
def say_hello():
print("Hello!")
say_hello()
# Output:
# Before the function call
# Hello!
# After the function call
# Access Modifiers in Python
class Example:
public_var = "Public"
_protected_var = "Protected"
__private_var = "Private"
e = Example()
print(e.public_var) # Accessible
print(e._protected_var) # Accessible but should not be used outside the class
# print(e.__private_var) # Error: Attribute not accessible