-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsection_05.py
More file actions
35 lines (28 loc) · 875 Bytes
/
section_05.py
File metadata and controls
35 lines (28 loc) · 875 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
# Getters and setter from properties rather than methods
# Protected by convention, single underscore
class User:
def __init__(self, username, email, password):
self._username = username
self._email = email
self._password = password
@property
def username(self):
return self._username
@property
def email(self):
print("In property setup")
return self._email
@email.setter
def email(self, value):
# Sample of validation in setter
if '@' in value:
self._email = value
@property
def password(self):
return self._password
user_01 = User("Dave", "aaa@corp.com", "abc")
print(user_01.email)
user_01.email = "Wont work"
print(user_01.email)
user_01.email = "bbb@firm.net" # Works, meets validation
print(user_01.email)