-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path5-propertys.py
More file actions
123 lines (89 loc) · 2.26 KB
/
5-propertys.py
File metadata and controls
123 lines (89 loc) · 2.26 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# a simple Property
# =================
class propertyFoo(object):
def __init__(self):
self._x = 10
@property
def X(self):
print "Using @property"
return self._x
@X.setter
def X(self, value):
self._x = value
print "using @X.setter"
foo = propertyFoo()
print foo.X
foo.X = 12
print foo.X
# ----------- clear -------------------
# propertys are usefull in case of Typefoo
class propertyFoo(object):
def __init__(self):
self._x = 10
@property
def X(self):
return self._x
@X.setter
def X(self, value):
if isinstance(value, str):
self._x = value
else:
raise Exception("Exception: Type Missmatch. Only INT allowed")
foo = propertyFoo()
print 4*"*" + " Now test if Integer works "
foo.X = 12
print foo.X
print 4*"*" + " Now test if String works "
foo.X = "FooBar"
print foo.X
# ------------------ clear -------------------
# a Property ListArray
class address(object):
def __init__(self):
self._ID = None
self._Name = None
self._Vorname = None
@property
def ID(self):
return self.__ID
@ID.setter
def ID(self, value):
if isinstance(value, int):
self._ID = value
else:
raise Exception("Exception: Type Missmatch. Only INT allowed")
@property
def Name(self):
return self._Name
@Name.setter
def Name(self, value):
if isinstance(value, str):
self._Name = value
else:
raise Exception("Exception: Type Missmatch. Only STR allowed")
@property
def Vorname(self):
return self._Vorname
@Vorname.setter
def Vorname(self, value):
if isinstance(value, str):
self._Vorname = value
else:
raise Exception("Exception: Type Missmatch. Only STR allowed")
adrList = []
adr1 = address()
adr1.ID = 0
adr1.Name = "Mustermann"
adr1.Vorname = "Hans"
adrList.append(adr1)
adr2 = address()
adr2.ID = 1
adr2.Name = "Hanswurst"
adr2.Vorname = "Mortima"
adrList.append(adr2)
for element in adrList:
print element
for element in adrList:
print element.ID, element.Vorname, element.Name