[Tutor] Self, Scopes and my unbelievable muddleheadedness.

doug shawhan doug.shawhan at gmail.com
Thu Oct 26 00:21:49 CEST 2006


I'm having a rather difficult time understanding the proper use of "self".

I have two functions (yes, they are ugly, I was getting ready to split them
in to smaller bits when this particular hole in my skull opened up) in a
module. They use the same list of dictionaries to create some tables in a
gadfly database.

I know I could fix this in any number of ways (including, but not limited
to, acting on the same input list twice, or acting on it in such a fashion
in the first place), but I am trying to actually understand "self".

Here's the horrid, horrid mess:

class Create:
    def freshDB(self, DBPATH, Fields):
        self.Fields = Fields
        for field in self.Fields.keys():
            columns = self.Fields[field]
            columns = columns.split(',')
            query = ''
            for each in columns:
                query = "%s, %s varchar"%(query,each)
            query = query[1:]
            query = "Create Table %s (%s)"%(field,query)
            self.Fields[field] = query

        connection = gadfly.gadfly()
        connection.startup("InventoryDB", DBPATH)
        cursor = connection.cursor()

        for field in self.Fields.keys():
            cursor.execute(self.Fields[field])
            connection.commit()

        for field in self.Fields.keys():
            cursor.execute("Select * from %s"%field)
            print cursor.pp()
        connection.close()

    def comparisonTable(self, DBPATH, Fields, columns, mode):
        query = ""
        if mode == 'new':
            connection = gadfly.gadfly("InventoryDB",DBPATH)
            cursor = connection.cursor()
            try:
                cursor.execute("drop table comparison")
            except:
                print "No old table found."
            columns = Fields["Common"].split(',')

            for each in columns:
                query = "%s, %s varchar"%(query,each)
            query = query[1:]
            query = "Create Table comparison (%s)"%query
            cursor.execute(query)
            connection.commit()
            cursor.execute("select * from comparison")
            print cursor.pp()

Now when I run freshDB from the other script:

Fields = {"Common":"Inventory_Number, Stock_Number, Location, Year, Make,
Model, Part_Type, Mileage, Description, Interchange_Data, Condition, Price",
    "EvilBay":"EvilBay_Title, EvilBay_Description",
    "HappyBase":"HappyBase_Description, HappyBase_Long_Description"}

d = DBMod

d.Create().freshDB(DBPATH, Fields)

d.Create().comparisonTable(DBPATH, Fields, columns, "new")


the resulting query is:

Create Table comparison ( Inventory_Number varchar,  Stock_Number varchar,
Location varchar,  Year varchar,  Make varchar,  Model varchar,  Part_Type
varchar,  Mileage varchar,  Description varchar,  Interchange_Data varchar,
Condition varchar,  Price varchar)

The query from comparisonTable gives me:
Create Table comparison ( Create Table Common ( Inventory_Number varchar
varchar,   Stock_Number varchar varchar,   Location varchar varchar,   Year
varchar varchar,   Make varchar varchar,   Model varchar varchar,
Part_Type varchar varchar,   Mileage varchar varchar,   Description varchar
varchar,   Interchange_Data varchar varchar,   Condition varchar varchar,
Price varchar) varchar)

If I restate the "Fields" input list before running comparisonTable, my
results are as expected (i.e. exactly like the first query, exept the table
is named "comparison").

I thought that when "self" was invoked for a function then that data was
acted on only for the scope of that function, yet the data is changed in the
original. Why?

I'm sure this is so obvious that a crack-addled tapeworm head down in a
bucket of stupid could understand it, unfortunately, I'm not quite at that
level today. Sorry.

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061025/afe84c8c/attachment.html 


More information about the Tutor mailing list