Chapter 1 - The Python Data Model
https://docs.python.org/3/reference/datamodel.html
Dunder methods: special methods such as
.__getitem__
ou.__dict__
French Deck example
class FrenchDeck:
ranks = [str(n) for n in range(2, 11)] + list('JQKA')
suits = 'spades diamonds clubs hearts'.split()
def __init__(self):
self._cards = [Card(rank, suit) for suit in self.suits
def __len__(self):
return len(self._cards)
def __getitem__(self, position):
return self._cards[position]
o uso dos atributos
.__getitem__
e.__len__
torna essa classe uma sequĂȘncia que pode ser iterable. EntĂŁo, podemos criar laços, sortear um item da instĂąncia classe etc.
MetĂłdo especial .__getitem__
.__getitem__
In Pythonâs data model, when you write foo[i]
, foo.__getitem__(i)
is called.
list = ['Carla','Leonardo','Carlos']
Slicing
a[start:stop] # items start through stop-1
a[start:] # items start through the rest of the array
a[:stop] # items from the beginning through stop-1
a[:] # a copy of the whole array
list = ['Carla','Leonardo','Carlos']
#input
list[:2]
['Carla', 'Leonardo']
Iterable
Just because a method implements .__getitem__
, it is also iterable
for i in list:
print(i)
Carla
Leonardo
Carlos
in
in
Uses the method .__contains__
'Carla' in list
True
Sorting
for i in sorted(list):
print(i)
Carla
Carlos
Leonardo
Chamada dos métodos especiais
No geral, não precisamos chamar diretamente os métodos especiais - quem faz isso é o interpretador python.
Um dos Ășnicos mĂ©todos que usamos diretamente Ă© o .__init__
- para invocar o inicializador da superclasse - Ă© um construtor de objetos.
Java
int age
int name
public void abc(int age,int name) {
this.age = age;
this.name = xyz;
}
Python
class Main:
def __init__(self):
self._age = None
self._name = None
def method(self):
print self._age, self._name
test = Main()
test.method()
# None None
* Stackoverflow: Quando devo usar init em funçÔes dentro de classes?
* Java Class Variables & init method in Python
Managed contexts
https://book.pythontips.com/en/latest/context_managers.html
Operators overloading
Em java, nĂŁo Ă© possĂvel fazer isso. EntĂŁo, temos que criar mĂ©todos especĂficos da classe para realizar essas operaçÔes (ex.:
sum
)
https://pense-python.caravela.club/17-classes-e-metodos/07-sobrecarga-de-operadores.html
Exemplo Vector
Vector
Nesse caso, implementamos operadores aritméticos para o formato de vetor e mostramos qual deve ser o comportamento se for chamada uma multiplicação ou adição de vetores.
from math import hypot class Vector:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __repr__(self):
return 'Vector(%r, %r)' % (self.x, self.y)
def __abs__(self):
return hypot(self.x, self.y)
def __bool__(self):
return bool(abs(self))
def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
return Vector(x, y)
def __mul__(self, scalar):
return Vector(self.x * scalar, self.y * scalar)
__string__
x __repr__
__string__
x __repr__
https://www.geeksforgeeks.org/str-vs-repr-in-python/
https://stackoverflow.com/questions/1436703/difference-between-str-and-repr
O __str__
serve para exibir o objeto para usuårio final, usada pelo comando print e pela função str.
O __repr__
serve para exibir o objeto para o programador, usada pelo console do Python e pela funçao repr.
* Dar preferĂȘncia Ă implementação do __repr__
- fallback
Method overloading
Diferentes formas de chamar o mesmo método.

https://pythonspot.com/method-overloading/
__bool__
and __len__
__bool__
and __len__
O tipo bool em python aceita qualquer tipo de objeto através de bool(x)
que chama x.__bool__()
.
Se o bool nĂŁo estiver implementado:
Python invoca
x.__len__()
(se retornar zero, serĂĄ false)
Why len is not a method
No method is called for the built-in objects of CPython: the length is simply read from a field in a C struct.
Last updated
Was this helpful?