Académique Documents
Professionnel Documents
Culture Documents
Mito:
Aula 15
Python 3.x
Python 3.x
Tipos bsicos - Classes
Python 2.x
class C: #classic class
class C(object): #new-style class
Python 3.x
class C(object): #new-style class
Aula 15
Python 3.x
Tipos bsicos - cmp e __cmp__
Python 2.x
>>> def reverse_numeric(x, y):!
return y - x!
>>> sorted([5, 2, 4, 1, 3], cmp=reverse_numeric)!
[5, 4, 3, 2, 1]!
Python 3.x
list.sort(key= cmp_to_key(reverse_numeric) )
Mtodo cmp no existe mais! Usar __lt__ ,
__eq__ with __hash__()
Aula 15
Python 3.x
Tipos bsicos - Strings
Python 2.x
str(anything)
unicode(anything)
Python 3.x
str(anything)
unicode(anything)
Aula 15
Python 3.x
Tipos bsicos - Inteiros e Longos
Python 2.x
x = 10000000000000000L
long(x)
Python 3.x
x = 10000000000000000
int(x)
No mais o tipo long! Todos so inteiros!
Aula 15
Python 3.x
Tipos bsicos - Octais
Python 2.x
x = O755
Python 3.x
x = 0o755
Octais sintaxe diferente!
Aula 15
Python 3.x
Tipos bsicos - Diviso
Python 2.x
x = 2/4
# Resp: 0
Python 3.x
x = 2/4 # Resp: 0.5
Diviso por Inteiros retornam Float!
Aula 15
Python 3.x
Tipos bsicos - Comparaes
Python 2.x
if x <> y: #ou
if x != y
Python 3.x
if x!=y
No h mais o operador <> !
Aula 15
Python 3.x
Tipos bsicos - Dicionrios
Python 2.x
a = {}
a.has_key( casa )
Python 3.x
casa in a
No h mais o mtodo has_key em Dicts!
Aula 15
Python 3.x
Tipos bsicos - Dicionrios
Python 2.x
a = { nome : marcel , idade : 26}
, marcel ),( idade , 26)]
a.items() # [( nome
a.keys() # [ nome , idade ]
Python 3.x
a = { nome : marcel , idade : 26}
list(a.items()) # [(
nome , marcel ),( idade , 26)]
list(a.keys()) # [ nome , idade ]
No retornam como listas, no podem ser
indexados!
Aula 15
Python 3.x
Tipos bsicos - next()
Python 2.x
iterator.next()
Python 3.x
next(iterator)
iterator.next()
H uma funo global next() para iteradores!
Aula 15
Python 3.x
Tipos bsicos - repr()
Python 2.x
`x` ou repr(x)
Python 3.x
repr(x)
`` para capturar representao no mais existe!
Aula 15
Python 3.x
Try ... Except..
Python 2.x
Python 3.x
try:
import
module
except
ImportError,
e:
pass
try:
import
module
except
ImportError
as e:
pass
Captura de erros e objetos com a keyword as
Aula 15
raise
Python 3.x
Python 2.x
raise MyException , mensagem de erro
Python 3.x
raise MyException (
mensagem de erro )
Aula 15
Python 3.x
List Comprehensions
Python 2.x
[ i for i in 1,2] #Isso TENSO
Python 3.x
[i for i in (1,2)]
Iterar sobre tuplas apenas com parnteses!
Aula 15
Python 3.x
I/O library
Python 2.x
a = open( arquivo.txt , w )
a.write( hello )
Python 3.x
with open( arquivo.txt , w) as f:
f.write( hello )
P
Aula 15
2to3 conversor
J instalado com seu Python 3.x ou encontrado em ../Tools/scripts
def greet(name):!
print "Hello, {0}!".format(name)!
print "What's your name?"!
name = raw_input()!
greet(name)!
$ 2to3 example.py!
def greet(name):!
print("Hello, {0}!".format(name))!
print("What's your name?")!
name = input()!
greet(name)!
http://docs.python.org/library/2to3.html
Aula 15
Python 3.x
Referncias
http://www.slideshare.net/jza/python-3000
http://diveintopython3.org/porting-code-to-python-3with-2to3.html
http://docs.python.org/library/2to3.html
Aula 15
Python 3.x