"doug shawhan" <[EMAIL PROTECTED]> wrote
> I'm having a rather difficult time understanding the proper use of 
> "self".

Doug, I think you may be having a more fundamental problem.
Looking at your code at the most superficial level it seems
you may not understand objects.

Classes are used to generate objects. Objects represent things.
By logical deduction objects correspond to nouns in language.
Objects do things when they receive messages (Possibly
from other objects). Messages cause methods to be invoked,
methods therefore correspond to verbs in language.

Looking at your code we find:

class Create:   # a verb
    def freshDB(self, DBPATH, Fields):   # a noun
    def comparisonTable(self, DBPATH, Fields, columns, mode): #a noun

In other words you have the concept inside out.
you are trying to create Create objects and call Table and Database
methods. It doesn't make sense.

Normally we would expect to see DB and Table objects to which
you send create messages.

Now if you think of the objects as verbs then self is a confusing 
concept.
But if you think of the objects as nouns then self is simply a 
reference
to the object instance in question, a particular database or table.

I'd recommend going back and rethinking the design of your
objects before going much further.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

>
> 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!
>


--------------------------------------------------------------------------------


> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to