Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
0
01 Python
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
PolySTAR
RoboMaster
Computer Vision
formations
01 Python
Commits
a810b485
Commit
a810b485
authored
5 years ago
by
Mathieu
Browse files
Options
Downloads
Patches
Plain Diff
[dataclasses] (correction) exo 2: use classvar instead of magic number
parent
cccefd02
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
d_dataclasses/02_person.py
+4
-2
4 additions, 2 deletions
d_dataclasses/02_person.py
with
4 additions
and
2 deletions
d_dataclasses/02_person.py
+
4
−
2
View file @
a810b485
...
...
@@ -5,7 +5,8 @@ We want the Person dataclass to have 3 attribute: the name, last name, and age o
But we want to be able to give the birthday (we limit ourselves to the year), and the class should determine the age on its own.
"""
import
doctest
from
dataclasses
import
dataclass
,
InitVar
from
dataclasses
import
InitVar
,
dataclass
from
typing
import
ClassVar
@dataclass
...
...
@@ -26,10 +27,11 @@ class Person:
age
:
int
=
None
birthday
:
InitVar
[
int
]
=
None
YEAR
:
ClassVar
[
int
]
=
2019
def
__post_init__
(
self
,
birthday
:
int
):
if
self
.
age
is
None
and
birthday
is
not
None
:
self
.
age
=
2019
-
birthday
self
.
age
=
self
.
YEAR
-
birthday
if
__name__
==
'
__main__
'
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment