You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: blog_files/pysheet.md
+35Lines changed: 35 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3936,6 +3936,41 @@ Our initial setup.py will also include information about the license and will re
3936
3936
3937
3937
Find more information visit [http://docs.python.org/install/index.html](http://docs.python.org/install/index.html).
3938
3938
3939
+
3940
+
## Dataclasses
3941
+
3942
+
```Dataclasses``` are python classes but are suitedfor storing data objects.
3943
+
This module provides a decoratorand functionsfor automatically adding generated special methods suchas```__init__()```and```__repr__()``` to user-defined classes.
3944
+
3945
+
### Features
3946
+
3947
+
1. They store dataand represent a certain datatype. Ex: A number. For people familiarwith ORMs, a model instanceis a dataobject. It represents a specific kind of entity. It holds attributes that defineor represent the entity.
3948
+
3949
+
2. They can be compared to other objects of the sametype. Ex: A number can be greater than, less than,or equal to another number.
3950
+
3951
+
Python3.7 provides a decorator dataclass thatis used to convert aclass into a dataclass.
3952
+
3953
+
python2.7
3954
+
```python
3955
+
>>>class Number:
3956
+
...def__init__(self, val):
3957
+
...self.val= val
3958
+
...
3959
+
>>>obj= Number(2)
3960
+
>>> obj.val
3961
+
2
3962
+
```
3963
+
with dataclass
3964
+
3965
+
```python
3966
+
>>>@dataclass
3967
+
...class Number:
3968
+
... val:int
3969
+
...
3970
+
>>>obj= Number(2)
3971
+
>>> obj.val
3972
+
2
3973
+
```
3939
3974
## Virtual Environment
3940
3975
3941
3976
The use of a Virtual Environmentis to test python codein encapsulated environmentsand to also avoid filling the base Python installationwith libraries we might usefor only one project.