Ok, I'm really really new to Python. I've installed Python 2.7 and is using IDLE (GUI).

I just wrote 2 classes, Car and CarsDB, but I can't load them together to use them.

class Car(object):
        'A car with its own information like make, year, model, price.'
       
        def __init__(self, mk, md, yr, pr):
            'Initializes car with the given information'
            self.make = mk
            self.model = md
            self.year = yr
            self.price = pr
           
        def showInfo(self):
            'Shows the car information such as make, model, year, price'
            print "Make: %s\nModel: %s\nYear: %d\nPrice: $%.2f\n" \
                  % (self.make, self.model, self.year, self.price)
class CarsDB(object):
    'A car DB with information such as make, model, year, price of each car.'
    
    def __init__(self):
        'Initialization'
        self.db = []
        
    def addCar(self, car):
        'Add a car to the DB'
        db.append(car)
        
    def showDB(self):
        "Shows the car's database"
        for eachCar in db:
            print eachCar,

This is what I get when I try to import those 2 classes to use (creating instances) in IDLE (the default GUI):
>>> import Car
>>> car = Car('Honda','Civic',2009,20000)

Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
car = Car('Honda','Civic',2009,20000)
TypeError: 'module' object is not callable

>>> import CarsDB
>>> db = CarsDB()

Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
db = CarsDB()
TypeError: 'module' object is not callable
>>>

Can someone tell me what I'm doing wrong? Or how to correctly load/use the classes?

This is not java. You can put any number of classes into one file. You do not have to put any classes into a file, like java does.
So the interpreter does not know what class you import by merely importing the module.

Assuming you have the two classes in a file called car.py.

from car import Car, CarsDB
or
import car
c=car.Car('Honda','Civic',2009,20000)

When you import, you import the module. A module can be a single python file or a directory containing __init__.py. Read the docs for that.

Note that you may have to append the path to the directory containing the file/module. An example of import

import sys
sys.path.append("/path/to/Car")
import Car      ## not Car.py as python appends the ".py"

Thanks slate. I didn't know that Python allows multiple classes in one file. This makes it easier for the classes to interact, now. Thanks :)

Thanks woooee for the path suggestion.

I have one question regarding multiple classes in one file. Would it be a good idea to put the test class for the that class in the same file (ie. TestCarsDB class together with the 2 classes above inside the same file)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.