Unconfigured Ad Widget

Collapse

Anúncio

Collapse
No announcement yet.

Criando variável e limpando console do Python interpreter.

Collapse
X
 
  • Filter
  • Tempo
  • Show
Clear All
new posts

  • Font Size
    #1

    Python Criando variável e limpando console do Python interpreter.

    Sabe aquela hora que seu console do Python fica cheio de coisas, sim existe uma maneira de limpar.

    Vamos lá.

    Código:
    class cls(object):
        def __repr__(self):
            import os
            os.system('cls' if os.name == 'nt' else 'clear')
            return ''
    
    cls = cls()
    Salve ele como o nome que quiser
    Você pode usar ele assim:

    C:\Users\Cr4t3r> python -i nomedoscript.py

    pronto, ele vai criar uma variável global sendo:

    cls: em caso de windows
    clear: em caso de sistemas Unix.

    Ao criar uma instância da classe cls e atribuir esta instância à variável global com o mesmo nome, toda vez que digitamos cls no prompt, para exibir o valor desta variável o Python executa repr(cls) e isso invoca o nosso método cls.__repr__(). Uma solução simples e inteligente, usando um pouco de conhecimento sobre o modelo de objetos da linguagem.

    Greetz to Luciano Ramalho, a propósito, sou seu Fã.
    Last edited by Vetus; 20-12-2013, 09:30.
    Yes, I am a criminal. My crime is that of curiosity. My crime is
    that of judging people by what they say and think, not what they look like.
    My crime is that of outsmarting you, something that you will never forgive me
    for.

    I am a hacker, and this is my manifesto. You may stop this individual,
    but you can't stop us all... after all, we're all alike.
    Similar Threads

  • Font Size
    #2
    nao seria mais prático usar o 'call' ?

    from subprocess import call
    call('clear')
    no windows... call('cls')
    sigpic

    Comment


    • Font Size
      #3
      Da também, mas aí prefiro o os:

      Código:
      import os
      os.system('clear')
      Yes, I am a criminal. My crime is that of curiosity. My crime is
      that of judging people by what they say and think, not what they look like.
      My crime is that of outsmarting you, something that you will never forgive me
      for.

      I am a hacker, and this is my manifesto. You may stop this individual,
      but you can't stop us all... after all, we're all alike.

      Comment

      X
      Working...
      X