forked from moskytw/examples-for-programming-with-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex_employee.py
More file actions
63 lines (45 loc) · 1.3 KB
/
ex_employee.py
File metadata and controls
63 lines (45 loc) · 1.3 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
58
59
60
61
62
63
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# file: ex_employee.py
class Employee(object):
# use `weakref` will be better
employees = set()
def __init__(self, name, title, id=None):
self.name = name
self.title = title
self.__class__.employees.add(self)
if id is None:
self._id = len(self.employees)
else:
self._id = id
def get_namecard(self):
return '{0.title} {0.name}'.format(self)
def fire(self):
self.__class__.employees.remove(self)
print 'DEBUG: I am fired. (name: %s)' % self.name
def __str__(self):
return self.name
def __repr__(self):
return 'Employee({0.name}, {0.title}, {0._id})'.format(self)
@property
def id(self):
return self._id
@classmethod
def fire_all(cls):
for employee in cls.employees.copy():
employee.fire()
if __name__ == '__main__':
andy = Employee('Andy', 'Sales')
bob = Employee('Bob', 'Engineer')
print andy
print bob
print
print andy.id, andy.name, andy.title
print [getattr(bob, attr_name) for attr_name in ('id', 'name', 'title')]
print
print andy.get_namecard()
print bob.get_namecard()
print
print Employee.employees
print
Employee.fire_all()