Results 1 to 4 of 4

Thread: Python: Object Oriented Programming

  1. #1
    Join Date
    Oct 2006
    Location
    Vandenberg AFB, CA
    Beans
    40
    Distro
    Ubuntu 7.04 Feisty Fawn

    Python: Object Oriented Programming

    I have a file ("data/map1.map") that is a cPickle dump an object Template(). This object has a num_layers field.

    Now let's say I have the following code, with DisplayMap.enter() as my entry point into my program (note - not complete code, just a portion of it:
    Code:
    import cPickle as pickle
    
    class DisplayMap(Scene):
        def enter(self):
            tiles = Tilemap('data/map1.map')
            tiles.display()
    
    class Tilemap:
        def __init__(self, filename):
            f = file(filename)
            self = pickle.load(f)
            print "\nTest #1:", self.num_layers
    
        def display(self):
            print "\nTest #2:", self.num_layers
    Why is it that Test #1 (found in Tilemap.__init__) responds correctly but test #2 (found in Tilemap.display) returns a AttributeError: Tilemap instance has no attribute 'num_layers'

    Is it because self is referring to something else, and not the tilemap instance of Tilemap()? What would it be referring to?

  2. #2
    Join Date
    Oct 2006
    Location
    Vandenberg AFB, CA
    Beans
    40
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Python: Object Oriented Programming

    I'm thinking because it's not going to be as simple for me to populate the object in-code with the object from file, am I going to have to cycle through all of the fields of the object in file (which has a lot more than just num_layers) to populate my new object in code?

    Is there some magical code snippet that will replicate an object for me?

  3. #3
    Join Date
    May 2005
    Beans
    Hidden!

    Re: Python: Object Oriented Programming

    yeah, .load() only gives one object at a time. You would need to run a while loop until you get an EOFError (that mean there are no more pickled objects left). This is what we have generators for

    That way, you could do just a for apickledobj in pickledfile('filename') to get all objects in unpickled form.

  4. #4
    Join Date
    Oct 2006
    Location
    Vandenberg AFB, CA
    Beans
    40
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Python: Object Oriented Programming

    I've only been using Python for a week - so I'm not quite following along with you.

    I think I forgot to mention that the pickle dump is of an entire class.

    I did manage to get it to work, I changed my Tilemap.__init__ function to the following:
    Code:
    def __init__(self, filename):
        f = file(filename)
        mapfile = pickle.load(f)
        self.num_layers = mapfile.num_layers
        self.layers = mapfile.layers
        self.imagemap = mapfile.imagemap
    I can then use the self. objects as I expected.

    Now I am attempting to get my dummy "map generator" to only save certain data out of the class and not the various functions it uses (which won't be present in it's final version).

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •