Jump to content

Python inheritance help


Recommended Posts

Here is the basic of what I have:

 

main.py

from Table import Table
if __name__ == "__main__":
    pb = Pybase("myDatabase")
    pb.delTbl("myDB")

 

Pybase.py

import os
class Pybase:
    dirLocation = "C:\\Users\\Administrator\\Desktop\\Database\\data\\"
    workDB = ""
    def __init__(self, db = ""):
        loc = self.dirLocation + db
        if os.path.exists(loc):
            self.workDB = loc
        else:
            print "Database: " + db + " does not exist."
    def mkDB(self, db):
        loc = self.dirLocation + db
        if os.path.exists(loc):
            print "Database: " + db + " already exists."
        else:
            os.mkdir(loc)
            print "Database: " + db + " created successfully."
    def delDB(self, db):
        loc = self.dirLocation + db
        if os.path.exists(loc):
            os.rmdir(loc)
            print "Database: " + db + " deleted successfully."
        else:
            print "Database: " + db + " does not exist."

 

Table.py

from Pybase import Pybase
class Table(Pybase):
    def __init__(self):
        val = ''
    def mkTbl(self, tbl):
        loc = self.workDB + tbl
        if os.path.exists(loc):
            print "Table: " + tbl + " already exists"
        else:
            print "Table: " + tbl + " created successfully"
    def delTbl(self, tbl):
        print ""
        

 

I'm not sure I understand why it is saying:

 

Traceback (most recent call last):

  File "C:\Users\Administrator\Desktop\Database\main.py", line 3, in <module>

    pb = Pybase("myDatabase")

NameError: name 'Pybase' is not defined

 

I thought I was doing inheritance correctly, but I guess not, does anyone know the proper way to do this?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/163733-python-inheritance-help/
Share on other sites

In Python 3.0, print is a function, not a construct.

 

 

(It just took me like 20 minutes to figure that out haha.)

 

 

http://docs.python.org/library/2to3.html

 

 

May be of use to you if you have a lot of Python 2 code and don't want to recode or switch to Python 2.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.