Python 3

1,507 views
1,376 views

Published on

A versão 3 do Python teve seu lançamento final 2008, mas ainda há muitas dúvidas se já está na hora de escrever aplicações usando python 3. Eu irei mostrar sobre as diferenças entre o python 2 e o 3, as novidades e como portar seu código para o python 3.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,507
On SlideShare
0
From Embeds
0
Number of Embeds
280
Actions
Shares
0
Downloads
29
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Python 3

    1. 1. python3@andrewsmedina
    2. 2. ‣ o que os outros pensam...globo .com
    3. 3. ‣ o que eu penso...globo .com
    4. 4. ‣ o que minha mãe pensa...globo .com
    5. 5. ‣ como realmente é!globo .com
    6. 6. python 2
    7. 7. ‣ pep8 ‣ generators ‣ zen of python ‣ iterators ‣ decorators ‣ comprehension ‣ descriptors ‣ abstract ‣ metaclass ‣ magic methods ‣ context manager ‣ subproccess ‣ multiproccessglobo .com
    8. 8. mas...
    9. 9. ‣ unicode ‣ classes new x old style ‣ // vs / ‣ print vs print() ‣ int vs long ‣ urllib, urllib2, urlparse ‣ xmlrpclib, DocXMLRPCServer, SimpleXMLRPCServerglobo .com
    10. 10. :(
    11. 11. python3 ‣ pep8 ‣ zen of python ‣ generators ‣ iterators ‣ objetosglobo .com
    12. 12. python3.0 2008..globo .com
    13. 13. python3.0 ‣ pip, distribute não funcionava no python3 ‣ 2to3 não foi suficienteglobo .com
    14. 14. python3.3 ‣ pip, distribute ‣ venv nativo ‣ 2to3, 3to2, six ‣ várias features já funcionam no python2.7globo .com
    15. 15. python3 no python2
    16. 16. divisão (python2) >>> 4 / 2 2 >>> 3 / 2 1globo .com
    17. 17. divisão (python3) >>> 3 / 2 1.5globo .com
    18. 18. divisão (python2) from __future__ import division >>> 3 / 2 1.5globo .com
    19. 19. divisão (python2) from __future__ import division >>> 3 // 2 1globo .com
    20. 20. string.format() “{0} - {1}”.format(“andrews”, 19) “{name} - {idade}”.format(name=”andrews”, idade=19)globo .com
    21. 21. comprehension {x for x in [1,2,3,3]}globo .com
    22. 22. comprehension {key.upper(): value for key, value in d.items()}globo .com
    23. 23. generatorsglobo .com
    24. 24. classes abstratasglobo .com
    25. 25. multiprocessingglobo .com
    26. 26. bytes e strings bytes para transferência string para representaçãoglobo .com
    27. 27. bytes e strings bytes (python3) == str (python2) string (python3 == bytes (python2)globo .com
    28. 28. bytes e strings (python2) u"andrews " + b"medina" u”andrews medina”globo .com
    29. 29. bytes e strings (python3) >>> u"andrews " + b"medina" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Cant convert bytes object to str implicitlyglobo .com
    30. 30. strings para bytes u"andrews ".encode(“utf-8”) + b"medina"globo .com
    31. 31. bytes para strings u"andrews " + b"medina".decode(“utf-8”)globo .com
    32. 32. print objeto novos parâmetros (sep, end, file, flush)globo .com
    33. 33. print (python2) >>> help(print) File "<stdin>", line 1 help(print) ^ SyntaxError: invalid syntaxglobo .com
    34. 34. print (python3) >>> help(print)globo .com
    35. 35. print (python2) >>> from __future__ import print_function >>> help(print)globo .com
    36. 36. print (python2) >>> print(", ".join(["banana", "batata"])) banana, batataglobo .com
    37. 37. print (python3) >>> alimentos = ["banana", "batata"] >>> print(*alimentos, sep=", ") banana, batataglobo .com
    38. 38. print (python3) from StringIO import StringIO out = StringIO() >>> print("ble", file=out) >>> out.getvalue() blenglobo .com
    39. 39. range, zip, map, filter retornam iteratorsglobo .com
    40. 40. range, zip, map, filter lista = list(range(10))globo .com
    41. 41. range, zip, map, filter for item in range(10): print itemglobo .com
    42. 42. expections except IOError as e:globo .com
    43. 43. class Class: new style por padrãoglobo .com
    44. 44. int int = longglobo .com
    45. 45. novidades
    46. 46. annotations adiciona meta dados em uma funçãoglobo .com
    47. 47. annotations def hello(name: str, age: int) -> int: print(name, age)globo .com
    48. 48. annotations >>> hello.__annotations__ {return: <class int>, name: <class str>, age: <class int>}globo .com
    49. 49. annotations >>> hello("ble", "ble") ble bleglobo .com
    50. 50. io io.FileIO io.StringIO io.BufferIOglobo .com
    51. 51. concurrent.future paralelismoglobo .com
    52. 52. concurrent.future interface Executorglobo .com
    53. 53. concurrent.future from concurrent.futures import ThreadPoolExecutor with ThreadPoolExecutor(max_workers=1) as executor: future = executor.submit(pow, 323, 1235) print(future.result())globo .com
    54. 54. concurrent.future from concurrent.futures import ProcessPoolExecutor with ProcessPoolExecutor(max_workers=4) as executor: future = executor.submit(pow, 323, 1235) print(future.result())globo .com
    55. 55. functools.lru_cache memoização nativaglobo .com
    56. 56. functools.lru_cache from functools import lru_cache @lru_cache(maxsize=None) def fib(n): if n < 2: return n return fib(n-1) + fib(n-2)globo .com
    57. 57. venv (virtualenv)globo .com
    58. 58. unittest(2)globo .com
    59. 59. unittest.mockglobo .com
    60. 60. pep 420 Implicit Namespace Packagesglobo .com
    61. 61. como portar
    62. 62. apenas python3globo .com
    63. 63. branches diferentes manter dois projetos :(globo .com
    64. 64. 2to3 convertor automágicoglobo .com
    65. 65. 2to3 print “ble” -> print(ble) except Exception, e -> except Exception as eglobo .com
    66. 66. 2to3 2to3=true #distributeglobo .com
    67. 67. 3to2globo .com
    68. 68. mesma base de código tratamento de excessõesglobo .com
    69. 69. six :)globo .com
    70. 70. leitura ‣ http://python3porting.com/ ‣ http://docs.python.org/3/ ‣ http://getpython3.com/diveintopython3/globo .com
    71. 71. perguntas?

    ×