-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes_scrap.py
141 lines (103 loc) · 3.12 KB
/
classes_scrap.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def momentum(mass, vel):
return mass * vel
def energy(mass, vel):
return 0.5 * mass * vel**2.0
mass1 = 1.0
vel1 = 0.5
mass2 = 3.0
vel2 = 0.7
print 'Particle 1:'
print 'Mass:', mass1
print 'Velocity:', vel1
print 'Momentum:', momentum(mass1, vel1)
print 'Energy:', energy(mass1, vel1)
print
print
print 'Particle 12:'
print 'Mass:', mass2
print 'Velocity:', vel2
print 'Momentum:', momentum(mass2, vel1)
print 'Energy:', energy(mass2, vel2)
# An alternative would be to use a dictionary for the attributes of the particle
def momentum_dict(particle):
return particle['mass'] * particle['vel']
def energy_dict(particle):
return 0.5 * particle['mass'] * particle['vel']**2.0
def init(mass, vel):
return {'mass': mass, 'vel': vel}
particles = [init(1.0, 0.5), init(3.0, 0.7)]
print momentum_dict(particles[0])
# Instead we can use a class
#class Particle(object):
#
# def __init__(self, mass, vel):
# self.mass = mass
# self.vel = vel
#
# def momentum(self):
# return self.mass * self.vel
#
# def energy(self):
# return 0.5 * self.mass * self.vel**2.
class Particle(object):
"""This is a particle object which uses descriptors to keep all attributes
in sync. It assumes that if energy or momentum change then it is velocity
(and not mass) which changed."""
def __init__(self, mass, vel):
self.__mass = mass
self.__vel = vel
self.__momentum = mass * vel
self.__energy = 0.5 * mass * vel**2.0
def set_mass(self, value):
self.__mass = value
self.__momentum = self.__mass * self.__vel
self.__energy = 0.5 * self.__mass * self.__vel**2.0
def set_vel(self, value):
self.__vel = value
self.__momentum = self.__mass * self.__vel
self.__energy = 0.5 * self.__mass * self.__vel**2.0
#def set_momentum(self, value):
# self.__momentum = value
# self.__vel = self.__momentum / self.__mass
# self.__energy = 0.5 * self.__mass * self.__vel**2.0
#def set_energy(self, value):
# self.__energy = value
# self.__vel = sqrt
def get_mass(self):
return self.__mass
def get_vel(self):
return self.__vel
def get_momentum(self):
return self.__momentum
def get_energy(self):
return self.__energy
mass = property(get_mass, set_mass)
vel = property(get_vel, set_vel)
momentum = property(get_momentum)
energy = property(get_energy)
# Magic Methods
# __repr__
# __add__
# __magic__
# Properties
class MyClass(object):
def __init__(self):
self._half_a = 14
def get_a(self):
print "In the getter"
return self._half_a * 2
def set_a(self, value):
print "In the setter"
self._half_a = value / 2.0
a = property(get_a, set_a)
class MyClass(object):
def __init__(self):
self._half_a = 14
@property
def a(self):
print "In the getter"
return self._half_a * 2
@a.setter
def a(self, value):
print "In the setter"
c = MyClass()