tutz

¿Cómo obtener ayuda en Python?

Cuando estamos trabajando en Python o en cualquier otro lenguaje, siempre necesitamos ayuda para recordar o conocer que hace hace tal función o que métodos podemos usar en determinados tipos de datos.

Bueno, para estos casos Python nos da una mano que es vital que lo sepas.

Función dir()

dir() es una potente función integrada en Python que nos devuelve la lista de atributos y métodos del objeto que le pasemos.

>>> nombre = 'Edwin'
>>> dir(nombre)
'__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Con el ejemplo anterior podemos ver rápidamente que métodos podemos usar para la variable que estamos usando.

Aunque no lo creas esta función es muy utilizada cuando recién estas iniciando en Python, úsala siempre que la necesites.

Función help()

Otra de las funciones de mucha ayuda que tiene Python es help(), su nombre lo dice todo.

Básicamente esta función nos devuelve documentación de lo que le pasemos.

Por ejemplo, si queremos saber sobre el método split de la variable que hemos definido líneas arriba

>>> help(nombre.split)
Help on built-in function split:

split(sep=None, maxsplit=-1) method of builtins.str instance
Return a list of the words in the string, using sep as the delimiter string.

    sep
      The delimiter according which to split the string.
      None (the default value) means split according to any whitespace,
      and discard empty strings from the result.
    maxsplit
      Maximum number of splits to do.
      -1 (the default value) means no limit.

Con esto ya sabemos que podemos hacer con esta función, de muchísima ayuda.

Te recomiendo que uses más seguido estas dos funciones (dir, help) te ayudaran a ser mejor desarrollador.

Si estas alternativas no te son suficientes tambien puedes escribirnos en nuestro twitter @tutz_tv y nosotros te ayudaremos lo antes posible ;).