Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ef11c96

Browse files
committed
Oops scripts added
1 parent 7daefb3 commit ef11c96

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

‎Oops.py‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
# PascalCase - EmployeeName , first letter capital
3+
# CamelCase - employeeName , first letter small
4+
5+
# Class always written in PascalCase
6+
7+
# Abstraction - Abstraction in action, provides an abstract method without showing its implementation
8+
# Encapsulation - Encapsulated method, abstracts how it works internally
9+
10+
'''
11+
We identify the following in our problem
12+
13+
Noun -> Class -> Employee
14+
15+
Adjective -> Attributes -> name,age,salary
16+
17+
Verbs -> Methods -> getSalary(), increment()
18+
'''
19+
20+
# Class attribute and instance attribute
21+
22+
# Class definition
23+
class Employee:
24+
company = "Google" # Class attribute - specific to each class
25+
26+
# Instantiation - creating an instance (object) of the class
27+
mahar = Employee() # Object instantiation
28+
29+
# Accessing class attribute using the instance
30+
print(mahar.company) # Output: "Google"
31+
32+
# Changing the class attribute for all instances of the class
33+
Employee.company = "Amazon"
34+
35+
# Adding instance attributes to the object
36+
mahar.name = "Mahar" # Instance attribute
37+
mahar.salary = "30,000" # Instance attribute
38+
39+
# Accessing instance attributes
40+
print(mahar.name) # Output: "Mahar"
41+
print(mahar.company) # Output: "Amazon" (class attribute can also be accessed through the instance)
42+
print(mahar.salary) # Output: "30,000"
43+
44+
45+
'''
46+
The 'self' parameter is used to refer to the instance itself,
47+
allowing access to its attributes and methods within the class.
48+
It's a way to make sure the instance itself is affected
49+
when modifying or accessing attributes or methods
50+
'''
51+
52+
class Employee:
53+
company = "Google"
54+
# Method to get and print the salary of an employee
55+
def getSalary(self):
56+
# Accessing and printing the company name and employee's salary
57+
print(f"Salary for this employee working in {self.company} is {self.salary}")
58+
59+
# Object instantiation
60+
mahar = Employee() # Creating an instance 'mahar' of the Employee class
61+
mahar.salary = 100000
62+
# Calling the 'getSalary' method for the 'mahar' instance
63+
# This will print the salary and company name using instance and class attributes
64+
mahar.getSalary() # Output: Salary for this employee working in Google is 100000
65+
66+
'''Static method
67+
Sometimes, we need a function that doesn’t use the self-parameter. We can define a static method like this:'''
68+
class MathOperations:
69+
@staticmethod
70+
def add(x, y):
71+
return x + y
72+
# Using the static method without creating an instance
73+
result = MathOperations.add(5, 7)
74+
print("Result of addition:", result) # Output: Result of addition: 12
75+
76+
77+
'''
78+
__init__() constructor
79+
__init__() is a special method that runs as soon as the object is created.
80+
It is known as the constructor and takes a self-argument and can also take further arguments.
81+
For Example:'''
82+
class Employee:
83+
def __init__(self, name, age):
84+
self.name = name
85+
self.age = age
86+
87+
def display_info(self):
88+
print(f"Name: {self.name}, Age: {self.age}")
89+
# Creating an object of the Employee class using the constructor (__init__ method)
90+
emp1 = Employee("Alice", 30)
91+
emp2 = Employee("Bob", 28)
92+
# Displaying employee information using instance method
93+
emp1.display_info() # Output: Name: Alice, Age: 30
94+
emp2.display_info() # Output: Name: Bob, Age: 28
95+
96+
97+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /