8.6. Operator Builtin

>>> from dataclasses import dataclass
>>>
>>>
>>> @dataclass
... class Vector:
...     x: int
...     y: int
...
...     def __float__(self):
...         return (self.x**2 + self.y**2)**0.5
...
...     def __str__(self): ...      # str(obj)
...     def __repr__(self): ...     # repr(obj)
...     def __format__(self, format_spec): ...  # format(obj, format_spec)
...
...     def __int__(self): ...      # int(obj)
...     def __float__(self): ...    # float(obj)
...     def __complex__(self): ...  # complex(obj)
...     def __bool__(self): ...     # bool(obj)
...     def __bytes__(self): ...    # bytes(obj)
...
...     def __round__(self, n): ...  # round(obj, n)
...     def __abs__(self): ...       # abs(obj)
...     def __ceil__(self): ...      # math.ceil(obj)
...     def __floor__(self): ...     # math.floor(obj)
...     def __len__(self): ...       # len(obj)
...     def __hash__(self): ...      # hash(obj)
...     def __del__(self): ...       # del obj
...
...     def __dir__(self): ...            # dir(obj)
...     def __divmod__(self, other): ...  # divmod(obj, other)
...
...     def __hex__(self): ...
...     def __ascii__(self): ...
...     def __oct__(self): ...
...     def __bin__(self): ...
...
...     def __next__(self): ...
...     def __iter__(self): ...
...     def __reversed__(self): ...
...
...     def __delattr__(self, item): ...
...     def __setattr__(self, key, value): ...
...     def __getattr__(self, item): ...
...     def __getattribute__(self, item): ...

8.6.1. Str

  • __str__

class User:
def __init__(self, firstname, lastname):

self.firstname = firstname self.lastname = lastname

def __str__(self) -> str:

return f'{self.firstname} {self.lastname}'

def __repr__(self) -> str:

clsname = self.__class__.__name__ firstname = self.firstname lastname = self.lastname return f'{clsname}({firstname=}, {lastname=})'

def __format__(self, format_spec) -> str:

return 'hello'

mark = User('Mark', 'Watney')

8.6.2. Repr

  • Dla programisty

# __format__

>>> mark
<__main__.User object at 0x119499880>
>>>
>>> repr(mark)
'<__main__.User object at 0x119499880>'
>>>
>>> mark.__repr__()
'<__main__.User object at 0x119499880>'
>>>
>>> f'cośtam {mark!r} cośtam'
'cośtam <__main__.User object at 0x119499880> cośtam'
>>> mark
User(firstname='Mark', lastname='Watney')
>>>
>>> repr(mark)
"User(firstname='Mark', lastname='Watney')"
>>>
>>> mark.__repr__()
"User(firstname='Mark', lastname='Watney')"
>>>
>>> f'cośtam {mark!r} cośtam'
"cośtam User(firstname='Mark', lastname='Watney') cośtam"

8.6.3. Str

  • dla użytkownika końcowego

>>> print(mark)
<__main__.User object at 0x119499880>
>>>
>>> str(mark)
'<__main__.User object at 0x119499880>'
>>>
>>> mark.__str__()
'<__main__.User object at 0x119499880>'
>>> f'cośtam {mark!s} cośtam'
'cośtam <__main__.User object at 0x119499880> cośtam'
>>>
>>>
>>> f'cośtam {mark} cośtam'
'cośtam <__main__.User object at 0x119499880> cośtam'
>>> print(mark)
Mark Watney
>>>
>>> str(mark)
'Mark Watney'
>>>
>>> mark.__str__()
'Mark Watney'
>>>
>>> f'cośtam {mark!s} cośtam'
'cośtam Mark Watney cośtam'

Wyjątek:

>>> f'cośtam {mark} cośtam'
'cośtam Mark Watney cośtam'

8.6.4. Format

  • Jak nie jest zdefiniowany, to używa __str__()

>>> format(mark)
'hello'
>>>
>>> f'cośtam {mark} cośtam'
'cośtam hello cośtam'
>>>
>>> f'cośtam {mark:coś} cośtam'
'cośtam hello cośtam'

#%%

SECOND = 1 MINUTE = 60 * SECOND HOUR = 60 * MINUTE DAY = 24 * HOUR WEEK = 7 * DAY YEAR = 365.25 * DAY MONTH = YEAR / 12 SOL = 24*HOUR + 39*MINUTE + 35.244*SECOND # dzień na Marsie

class Mission:
def __init__(self, name, duration):

self.name = name self.duration = duration

def __format__(self, unit):

duration = self.duration match unit:

case 's' | 'seconds': duration /= SECOND case 'm' | 'minutes': duration /= MINUTE case 'h' | 'hours': duration /= HOUR case 'd' | 'days': duration /= DAY case 'w' | 'weeks': duration /= WEEK case 'M' | 'months': duration /= MONTH case 'y' | 'years': duration /= YEAR case _: duration /= SOL; unit = 'sol'

return f'{duration:.1f} {unit}'

ares3 = Mission('Ares3', duration=543*SOL)

print(f'Misja Ares3 trwała: {ares3}') # Misja Ares3 trwała: <__main__.Mission object at 0x11d0ad460>

print(f'Misja Ares3 trwała: {ares3}') # Misja Ares3 trwała: 48204957.492000006

print(f'Misja Ares3 trwała: {ares3}') # Misja Ares3 trwała: 48204957.5 seconds

>>> print(f'Misja Ares3 trwała: {ares3}')
Misja Ares3 trwała: 543.0 sol
>>>
>>> print(f'Misja Ares3 trwała: {ares3:seconds}')
Misja Ares3 trwała: 48204957.5 seconds
>>>
>>> print(f'Misja Ares3 trwała: {ares3:days}')
Misja Ares3 trwała: 557.9 days
>>>
>>> print(f'Misja Ares3 trwała: {ares3:months}')
Misja Ares3 trwała: 18.3 months
>>>
>>> print(f'Misja Ares3 trwała: {ares3:years}')
Misja Ares3 trwała: 1.5 years
>>>
>>> print(f'Misja Ares3 trwała: {ares3:weeks}')
Misja Ares3 trwała: 79.7 weeks

#%%

>>> d
datetime.date(2024, 4, 17)
>>>
>>>
>>> f'dziś jest {d}'
'dziś jest 2024-04-17'
>>>
>>> f'dziś jest {d:%B}'
'dziś jest April'
>>>
>>> f'dziś jest {d:%B %d}'
'dziś jest April 17'
>>> f'dziś jest {d:%B %d, %Y}'
'dziś jest April 17, 2024'