Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
kurs_alx_pcz
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
kurs_alx_pcz
Commits
37505baf
Commit
37505baf
authored
Dec 01, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
magiczne metody
parent
0cf59f3d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
0 deletions
+41
-0
magiczne_metody.py
dzien7/magiczne_metody/magiczne_metody.py
+41
-0
No files found.
dzien7/magiczne_metody/magiczne_metody.py
0 → 100644
View file @
37505baf
class
Liczba
:
def
__init__
(
self
,
x
=
0
):
self
.
x
=
x
# repr ma zwrócić tekstową postać obiektu, ale zapisaną "technicznie"
# tak, aby dało się to wkleić do kodu Pythona
def
__repr__
(
self
):
return
f
'Liczba({self.x})'
# str ma zwrócić tekstową postać obiektu, ale przeznaczoną dla człowieka
def
__str__
(
self
):
return
f
'[{self.x}]'
# Aby określić, jak ma działać dodawanie (operator +) dla obiektów naszej klasy,
# definiujemy metodę __add__, gdzie lewy i prawy argument są przekazywane
# jako self i other
def
__add__
(
self
,
other
):
return
Liczba
(
self
.
x
+
other
.
x
)
# sub, mul, truediv, floordiv, mod
# wersje z literą 'r' na początku - odwrócona kolejność argumentów
# wersja z 'i' na początku - zmiana wartości "w miejscu", czyli + , *= itp.
def
__eq__
(
self
,
other
):
return
self
.
x
==
other
.
x
# lt, le, gt, ge, ne do pozostałych porównań
# ne nie trzeba implementować, bo jest negacją eq
liczba1
=
Liczba
(
3
)
liczba2
=
Liczba
(
5
)
print
(
'str:'
,
str
(
liczba1
))
print
(
'repr:'
,
repr
(
liczba1
))
print
()
print
(
liczba1
,
liczba2
)
suma
=
liczba1
+
liczba2
print
(
'suma:'
,
suma
,
'typu'
,
type
(
suma
))
print
(
suma
==
Liczba
(
8
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment