Created
November 10, 2011 20:49
-
-
Save tiagodavi/1356173 to your computer and use it in GitHub Desktop.
Exemplo de *args e **kwargs (argumentos ilimitados, nomeados e em qualquer ordem)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
#Exemplo de *args e **kwargs (argumentos ilimitados, nomeados e em qualquer ordem) | |
class Conta(): | |
#É precico passar o self para dentro dos métodos de instância | |
#O Python é bem explícito :) | |
def metodo(self, *args, **kwargs): | |
print args | |
print kwargs | |
#c é uma instância de conta | |
c = Conta() | |
#=> tupla (1,2,3) | |
#=> dicionário vazio{} | |
c.metodo(1,2,3) | |
#=> tupla (1,2,3) | |
#=> dicionário {'nome':'tiago davi'} | |
#Pode fazer isso print kwargs['nome'] e imprimir o nome lá no método | |
c.metodo(1,2,3, nome = 'tiago davi') | |
#=> tupla ([0,1,2,3,4,5], 10) | |
#=> dicionário {'nome':'tiago davi', 'cargo':'programador','local':'RJ'} | |
c.metodo(range(0,6), 10, nome = 'tiago davi', cargo = 'programador', local = 'RJ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment