Skip to content

Commit 623e004

Browse files
Property in Python
1 parent 8a17bb0 commit 623e004

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

lectures-code/property-1.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of Python course available on CourseGalaxy.com
3+
4+
class Product:
5+
def __init__(self,x,y):
6+
self._x = x
7+
self._y = y
8+
9+
def display(self):
10+
print(self._x, self._y)
11+
12+
@property
13+
def value(self):
14+
return self._x
15+
16+
@value.setter
17+
def value(self, val):
18+
self._x = val
19+
20+
@property
21+
def y(self):
22+
return self._y
23+
24+
@y.setter
25+
def y(self, val):
26+
self._y = val
27+
28+
29+
30+
p = Product(12,24)
31+
print(p.value)
32+
print(p.value + 2)
33+
p.value = 10
34+
print(p.y)
35+
p.y = 12
36+
print(p.y)

lectures-code/property-2.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of Python course available on CourseGalaxy.com
3+
4+
class Person:
5+
def __init__(self, name, age):
6+
self.name = name
7+
self.age = age
8+
9+
def display(self):
10+
print(self.name,self.age)
11+
12+
@property
13+
def age(self):
14+
return self._age
15+
16+
@age.setter
17+
def age(self, new_age):
18+
if 20< new_age<80:
19+
self._age = new_age
20+
else:
21+
raise ValueError('Age must be between 20 and 80')
22+
23+
p = Person('Peter', 30)
24+
print(p.age)
25+
p.age = 200
26+
print(p.age)
27+
28+
p1 = Person('Dev',200)
29+
p.age = p.age +1
30+
p.age += 1
31+
print(p.age)

lectures-code/property-3.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of Python course available on CourseGalaxy.com
3+
4+
class Employee:
5+
def __init__(self, name, password, salary):
6+
self._name = name
7+
self._password = password
8+
self._salary = salary
9+
10+
@property
11+
def name(self):
12+
return self._name
13+
14+
@property
15+
def password(self):
16+
raise AttributeError('password not readable')
17+
18+
@password.setter
19+
def password(self, new_password):
20+
self._password = new_password
21+
22+
@property
23+
def salary(self):
24+
return self._salary
25+
26+
@salary.setter
27+
def salary(self, new_salary):
28+
self._password = new_salary
29+
30+
31+
e = Employee('Jill', 'feb31', 5000)
32+
print(e.name)
33+
#e.name = 'dd'
34+
#print(e.password)
35+
e.password = 'feb29'
36+
print(e.salary)
37+
e.salary = 6000

lectures-code/property-4.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of Python course available on CourseGalaxy.com
3+
4+
class Rectangle():
5+
def __init__(self,length,breadth):
6+
self.length = length
7+
self.breadth = breadth
8+
9+
def area(self):
10+
return self.length * self.breadth
11+
12+
def perimeter(self):
13+
return 2*(self.length + self.breadth)
14+
15+
@property
16+
def diagonal(self):
17+
return (self.length*self.length + self.breadth * self.breadth)**0.5
18+
19+
r = Rectangle(2,5)
20+
print(r.diagonal)
21+
r.length = 10
22+
print(r.diagonal)
23+
24+

lectures-code/property-5.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of Python course available on CourseGalaxy.com
3+
4+
class Product:
5+
def __init__(self,x,y):
6+
self._x = x
7+
self._y = y
8+
9+
def display(self):
10+
print(self._x, self._y)
11+
12+
@property
13+
def value(self):
14+
return self._x
15+
16+
@value.setter
17+
def value(self, val):
18+
self._x = val
19+
20+
@value.deleter
21+
def value(self):
22+
print('value deleted')
23+
24+
@property
25+
def y(self):
26+
return self._y
27+
28+
@y.setter
29+
def y(self, val):
30+
self._y = val
31+
32+
p = Product(12,24)
33+
del p.value
34+

0 commit comments

Comments
 (0)