1
+ class User :
2
+ species = 'human'
3
+ def __init__ (self , username , email ):
4
+ self .username = username
5
+ self .email = email
6
+ def __str__ (self ):
7
+ return self .username
8
+
9
+ class SuperUser (User ):
10
+ def __init__ (self , username , email ):
11
+ super ().__init__ (username , email )
12
+
13
+
14
+
15
+ bruce = SuperUser (
'criticbruce' ,
'[email protected] ' )
16
+ print (bruce .species )
17
+ Kacey = User (
'demo' ,
'[email protected] ' )
18
+ print (Kacey .species )
19
+ print (type (bruce ))
20
+ print (bruce )
21
+
22
+
23
+
24
+ # class Circle:
25
+ # pi = 3.14
26
+ # def __init__(self, radius):
27
+ # self.radius = radius
28
+
29
+ # @staticmethod
30
+ # def circumference(self, radius):
31
+ # c = 2 * self.pi * radius
32
+ # return Circle(c)
33
+
34
+ # egg = Circle(6)
35
+ # print(egg.circumference(egg.radius))
36
+
37
+
38
+ # def distance(p1, p2):
39
+ # dx = p1['x'] - p2['x']
40
+ # dy = p1['y'] - p2['y']
41
+ # return math.sqrt(dx*dx + dy*dy)
42
+
43
+ # p1 = {'x': 5, 'y': 2}
44
+ # p2 = {'x': 8, 'y': 4}
45
+ # print(distance(p1, p2))
46
+
47
+ class Point :
48
+ def __init__ (self , x , y ): # this is the initializer
49
+ self .x = x # these are member variables
50
+ self .y = y
51
+
52
+ def distance (self , p ): # method, or 'member function'
53
+ dx = self .x - p .x
54
+ dy = self .y - p .y
55
+ return math .sqrt (dx * dx + dy * dy )
56
+
57
+ # p1 = Point(5, 2) # call the initializer, instantiate the class
58
+
59
+ # p1 = (5,2)
60
+ # p2 = Point(8, 4)
61
+
62
+ # # print(p1.x) # 5
63
+ # # print(p1.y) # 2
64
+ # print(type(p1)) # Point
65
+ # print(p1.distance(p2))
66
+
67
+
68
+
69
+
70
+
71
+ import math
72
+
73
+ class Point :
74
+ def __init__ (self , x = 0 , y = 0 ):
75
+ self .x = x
76
+ self .y = y
77
+
78
+ def distance (self , p ): # method, or 'member function'
79
+ dx = self .x - p .x
80
+ dy = self .y - p .y
81
+ return math .sqrt (dx * dx + dy * dy )
82
+
83
+ def scale (self , v ):
84
+ self .x *= v
85
+ self .y *= v
86
+
87
+ # p3 = Point()
88
+ # p2 = Point(8,4)
89
+ # dist = p1.distance(p2) # or p2.distance(p1), either works
90
+ # print(dist)
0 commit comments