21.12.2022 Views

python_para_desenvolvedores_2ed

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Propriedades 125

# -*- coding: latin1 -*-

# Exemplo de property de leitura

class Projetil(object):

def __init__(self, alcance, tempo):

self.alcance = alcance

self.tempo = tempo

@property

def velocidade(self):

return self.alcance / self.tempo

moab = Projetil(alcance=10000, tempo=60)

# A velocidade é calculada

print moab.velocidade

Saída:

166

Exemplo de propriedade através de chamada de função:

# Property de leitura e escrita

class Projetil(object):

def __init__(self, alcance, tempo):

self.alcance = alcance

self.tempo = tempo

# Calcula a velocidade

def getv(self):

return self.alcance / self.tempo

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!