Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit63eb119

Browse files
authored
Merge pull requestwilfredinni#32 from Sonatrix/master
Add dataclass basic intro
2 parents2e15ba1 +fa3642f commit63eb119

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

‎blog_files/pysheet.md‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3936,6 +3936,41 @@ Our initial setup.py will also include information about the license and will re
39363936

39373937
Find more information visit [http://docs.python.org/install/index.html](http://docs.python.org/install/index.html).
39383938

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+
```
39393974
## Virtual Environment
39403975

39413976
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.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp