5

I'm really new to python and I have made the following program:

class AddressBook:
    def __init__(self):
        self.b = {}

    def insert(self,name, phone):
        self.b[name]=phone
        print "I am confused"

    def get(self,name):
        return self.b[name]

    def has_name(self,name):
        return self.b.has_key(name)

    def list(self):
        for n,p in self.b.iteritems():
            print n,p

    def delete(self, name):
        del self.b[name]

    def orderedList(self):
        orderedkeys = self.b.keys()
        orderedkeys.sort()
        for n in orderedkeys:
            print n, self.b[n]

I now want to compile it test it out in terminal to see if it all works. I went to the directory and compiled it with

python address.py

Now I want to add things to the list, print the contents of the list, delete them (pretty much play around with my program) but I don't know how...

After compiling, how do I manually test (play around) with my python program?

Thanks in advance.

Adam Goldberg
  • 223
  • 5
  • 16
  • 12
    You don't *compile* your python program. To test it, just do `python -i address.py` – Kh40tiK Nov 26 '16 at 15:25
  • 2
    This is a good question, especially for beginners. It demonstrates a solid effort and a sincere inquiry. Seasoned Pythonistas tend to forget what it's like as a beginner. And it turns out, searching for a direct answer is not trivial. Thanks for the post. – pylang Nov 26 '16 at 16:22
  • Thank you @pylang – Adam Goldberg Nov 26 '16 at 17:03

2 Answers2

10

Python is an interpreted language, and .py files do not require direct compilation. There are a few ways to run Python code, but for "playing around" you can simply activate the Python interpreter and import the class.

In a command prompt:

> python

In Python:

>>> from address import AddressBook
>>> a = Addressbook()
>>> a.insert("Jenny", "867-5309")
>>> a.get("Jenny")
'867-5309'
pylang
  • 40,867
  • 14
  • 129
  • 121
  • 3
    This is a good answer (with a good link). If you are writing code for nonprogrammers to use then you need to think of a way for them to use it (e.g. tkinter wrapper), but for your own use or for other Python programmers, the Python shell is often more convenient than e.g. as a command line script. When I write something for my own use I seldom bother with anything other than functions/ classes to be directly invoked from the shell. – John Coleman Nov 26 '16 at 17:01
4

The python script is not compiled. At least not in ways as other languages, like Fortran and C. From this answer:

Python has a compiler! You just don't notice it because it runs automatically. You can tell it's there, though: look at the .pyc (or .pyo if you have the optimizer turned on) files that are generated for modules that you import.

Also, it does not compile to the native machine's code. Instead, it compiles to a byte code that is used by a virtual machine. The virtual machine is itself a compiled program. This is very similar to how Java works; so similar, in fact, that there is a Python variant (Jython) that compiles to the Java Virtual Machine's byte code instead! There's also IronPython, which compiles to Microsoft's CLR (used by .NET). (The normal Python byte code compiler is sometimes called CPython to disambiguate it from these alternatives.)

You have two ways to test it out:

  1. type python -i address.py in the terminal. This will run the script and enter the python shell.

  2. You enter the python shell and then type from address.py import AddressBook.

On both ways, you can play around with your code.

Community
  • 1
  • 1
gabra
  • 9,484
  • 4
  • 29
  • 45