1
+ class Book ():
2
+ def __init__ (self , title , author , price ):
3
+ super ().__init__ ()
4
+ self .title = title
5
+ self .author = author
6
+ self .price = price
7
+
8
+ def __str__ (self ):
9
+ return f'{ self .title } by { self .author } , costs { self .price } '
10
+
11
+ def __repr__ (self ):
12
+ return f'title={ self .title } ,author={ self .author } ,price={ self .price } '
13
+
14
+
15
+ book1 = Book ('Python Crash Course' , 'Eric Matthes' , 23.99 )
16
+ book2 = Book ('Serious Python' , 'Julien Danjou' , 25.43 )
17
+
18
+ # print each object
19
+ print (book1 )
20
+ print (book2 )
21
+ # Python Crash Course by Eric Matthes, costs 23.99
22
+ # Serious Python by Julien Danjou, costs 25.43
23
+
24
+ # use str() and repr()
25
+ print (str (book1 ))
26
+ print (repr (book2 ))
27
+ # Python Crash Course by Eric Matthes, costs 23.99
28
+ # title=Serious Python,author=Julien Danjou,price=25.43
29
+
30
+
31
+ from dataclasses import dataclass
32
+
33
+
34
+ @dataclass
35
+ class Book ():
36
+ title : str
37
+ author : str
38
+ price : float
39
+
40
+ def __str__ (self ):
41
+ return f'{ self .title } by { self .author } , costs { self .price } '
42
+
43
+ def __repr__ (self ):
44
+ return f'title={ self .title } ,author={ self .author } ,price={ self .price } '
45
+
46
+
47
+ book1 = Book ('Python Crash Course' , 'Eric Matthes' , 23.99 )
48
+ book2 = Book ('Serious Python' , 'Julien Danjou' , 25.43 )
49
+
50
+ # print each object
51
+ print (book1 )
52
+ print (book2 )
53
+ # Python Crash Course by Eric Matthes, costs 23.99
54
+ # Serious Python by Julien Danjou, costs 25.43
55
+
56
+ # use str() and repr()
57
+ print (str (book1 ))
58
+ print (repr (book2 ))
59
+ # Python Crash Course by Eric Matthes, costs 23.99
60
+ # title=Serious Python,author=Julien Danjou,price=25.43
61
+
62
+
63
+ from dataclasses import dataclass
64
+
65
+
66
+ @dataclass
67
+ class Book :
68
+ title : str
69
+ author : str
70
+ pages : int
71
+ price : float
72
+
73
+ # the __post_init__ function lets us customize additional properties
74
+ # after the object has been initialized via built-in __init__
75
+ def __post_init__ (self ):
76
+ self .description = f'{ self .title } by { self .author } , { self .pages } pages'
77
+
78
+
79
+ # create some Book objects
80
+ book1 = Book ('Python Crash Course' , 'Eric Matthes' , 544 , 23.99 )
81
+ book2 = Book ('Serious Python' , 'Julien Danjou' , 225 , 25.43 )
82
+
83
+ # use the description attribute
84
+ print (book1 .description )
85
+ print (book2 .description )
86
+ # Python Crash Course by Eric Matthes, 544 pages
87
+ # Serious Python by Julien Danjou, 225 pages
88
+
89
+
90
+ from dataclasses import dataclass , field
91
+
92
+
93
+ @dataclass
94
+ class Book :
95
+ # you can define default values when attributes are declared
96
+ title : str = 'Empty Book'
97
+ author : str = 'Your Imagination'
98
+ pages : int = 0
99
+ price : float = 0.0
100
+
101
+
102
+ # Create a default book object
103
+ book1 = Book ()
104
+ print (book1 )
105
+
106
+ # Create a specified book, price is set by field operator
107
+ book1 = Book ('Python Crash Course' , 'Eric Matthes' , 544 , 23.99 )
108
+ book2 = Book ('Serious Python' , 'Julien Danjou' , 225 , 25.43 )
109
+ print (book1 )
110
+ print (book2 )
111
+ # Book(title='Empty Book', author='Your Imagination', pages=0, price=0.0)
112
+ # Book(title='Python Crash Course', author='Eric Matthes', pages=544, price=23.99)
113
+ # Book(title='Serious Python', author='Julien Danjou', pages=225, price=25.43)
114
+
115
+
116
+ from dataclasses import dataclass
117
+
118
+
119
+ @dataclass (frozen = True )
120
+ class Book :
121
+ title : str
122
+ author : str
123
+ pages : int
124
+ price : float
125
+
126
+ # You can define methods in a dataclass like any other
127
+ def bookinfo (self ):
128
+ return f'{ self .title } , by { self .author } '
129
+
130
+
131
+ # create some instances
132
+ book1 = Book ('Python Crash Course' , 'Eric Matthes' , 544 , 23.99 )
133
+ book2 = Book ('Serious Python' , 'Julien Danjou' , 225 , 25.43 )
134
+
135
+ # access fields
136
+ print (book1 .title )
137
+ print (book2 .author )
138
+
139
+ # print the book itself - dataclasses provide a default
140
+ # implementation of the __repr__ function
141
+ print (book1 )
142
+
143
+ # comparing two dataclasses
144
+ book3 = Book ('Automate the Boring Stuff with Python' , 'Al Sweigart ' , 592 , 26.99 )
145
+ print (book1 == book3 )
146
+
147
+ # change some fields, call a regular class method
148
+ book1 .title = 'Python for Kids'
149
+ book1 .pages = 864
150
+ print (book1 .bookinfo ())
151
+ # Python Crash Course
152
+ # Julien Danjou
153
+ # Book(title='Python Crash Course', author='Eric Matthes', pages=544, price=23.99)
154
+ # False
155
+ # Traceback (most recent call last):
156
+ # File "C:/python/OOP/dataclass.py", line 33, in
157
+ # book1.title = 'Python for Kids'
158
+ # File "", line 3, in __setattr__
159
+ # dataclasses.FrozenInstanceError: cannot assign to field 'title'
0 commit comments