[Tutor] Question About Subclasses
Charlie Derr
charlie@webmind.com
Tue, 27 Feb 2001 20:22:57 -0500
try
cupboard = Classes.Containers("cupboard", 0, "closed")
it will also work without that change if you subsitute
from Classes import *
for
import Classes
but there are a bunch of reasons for *not* doing it this way -- get in the
habit of using the syntax in the first solution
hth,
~c
|-----Original Message-----
|From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
|Britt Green
|Sent: Tuesday, February 27, 2001 7:55 PM
|To: tutor@python.org
|Subject: [Tutor] Question About Subclasses
|
|
|Hello,
|
|I'm having some problems getting Python to create a subclass. I
|have a class
|called Items, and then a subclass of that called Containers.
|Python doesn't
|recognize Containers as a valid subclass.
|
|This is what I have in a file called "Classes.py":
|
|class Items:
| def __init__(self, name, ip):
| self.name = name
| self.inPossesion = ip
|
|class Containers(Items):
| def __init__(self, name, ip, oc):
| Items.__init__(self, name, ip)
| self.openClosed = oc
|
|Then within another file called "game.py" I have the following code:
|
|import Classes
|
|cupboard = Containers("cupboard", 0, "closed")
|
|key = Items ("worn key", 0)
|
|When I go to run this code, Python spits this back at me:
|
|>>>
|Traceback (innermost last):
| File "C:/Program Files/Python20/game.py", line 3, in ?
| cupboard = Containers("cupboard", 0, "closed")
|NameError: There is no variable named 'Containers'
|
|What am I doing wrong?
|
|Thanks,
|
|Britt
|
|--
|It is pitch black. You are likely to be eaten by a grue.
|
|_________________________________________________________________
|Get your FREE download of MSN Explorer at http://explorer.msn.com
|
|
|_______________________________________________
|Tutor maillist - Tutor@python.org
|http://mail.python.org/mailman/listinfo/tutor