Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Lists Removing Items From A List

Minwook Jeong
Minwook Jeong
2,453 Points

Can anyone tell me about the function of clear()

Kenneth has set up the clear() function

eg)

import os def clear(): os.system("cls") if os.name == "nt" else 'clear'

Can anyone tell me how this function works?

1 Answer

Hi there!

I'll do my best. First thing to understand is that the clear function doesn't actually clear the terminal. If you were to launch python and paste in this function into the interpreter and call it, it wouldn't work. Actually if you go here: https://repl.it/languages/python3 And try it out, you'll see it's not clearing the screen.

That's because this works by telling the operating system's command line to do something for python. if you're on Windows open up cmd.exe and type "dir" you'll be shown the contents of the current folder you find yourself in. If you're on a unix derived system - linux, Mac, BSD, actually pretty much all other systems used in desktops and severs currently you just open up your command line and type "ls" to do the same thing.

Now to clear the screen of that information, on windows you type "cls" and on any of the others you type "clear". The way that's implemented varies - the screen could be "cleared" or a load of empty lines could be put in so it looks like it's cleared, but the point is that on windows the command is "cls" and on much everything else it's "clear" and of course that these commands aren't inside python but external to it.

What we use here is a python module called os.py. It's part of the standard library, so pretty much all python installs will have it. os gives us a lot of useful tools we can use to interact with the operating system that python is running on. We need this because python is designed to run independent of operating system or hardware - python is python whether it's on a Mac, Windows or a Raspberry Pi. From this module we only need two things: os.name and os.system, so we could have imported them like this:

from os import name, system

What os.name does is tell us in a very general way what type of operating system python is running on. These are very broad definitions - for example linux, mac and bsd all come up as 'posix', and all versions of windows (XP and later) will come up as "nt". Because Windows is the only one that has a different command to clear the screen, that's the only one we check for. we say 'do x if the operating system is 'nt', otherwise do y' (Bonus points, if you wanted to tell the difference between mac, linux, bsd, etc you could use sys.platform, but we don't need that here)

To do x and y, we need a way for python to tell the command line to do the same thing as if we typed cls or clear. The way we do that is with os.system. system() takes an argument, and passes that to the command line as a command. So for windows systems we do system("cls") and for posix systems we do system("clear").

Hope it helps, sorry if it's way more detail than you were looking for!

Jon

Minwook Jeong
Minwook Jeong
2,453 Points

Such a long answer! impressed! I am fully understood!