-1

So I am trying to have a file that has a bunch of object in it that looks like this:

<class 'oPlayer.oPlayer'>,123,4,<class 'CommonObject.Wall'>,175,4,<class 'CommonObject.Wall'>,25,654,<class 'CommonObject.Wall'>,1,123,<class 'CommonObject.Wall'>,55,87

(No newlines for splitting purposes)

The file holds the object name, x, and y coordinate. (Basic info) But I'm not 100% sure how to create the objects from the file. Here is what I have:

def loadRoom(self, fname):

    # Get the list of stuff
    openFile = open(fname, "r")
    data = openFile.read().split(",")
    openFile.close()

    # Loop through the list to assign said stuff
    for i in range(len(data) / 3):

        # Create the object at the position
        newObject = type(data[i * 3])
        self.instances.append(newObject(int(data[i * 3 + 1]), int(data[i * 3 + 2])))

The objects in the file all take two arguments, x and y. So I'm also confused on how that would work. What I did was grab the list with all the split strings, (Which I displayed, it came out correct. No \n's) then I loop through the list (sort of) to set all the data. I assumed that type would return the object, but it doesn't.

Any help with said topic is very appreciated.

5
  • 5
    Where did this file come from? Where are the classes defined? What's going on here?! Are you just looking for pickle? Commented Apr 25, 2015 at 4:40
  • try built-in type function, see my answer here stackoverflow.com/a/8576049/29489 Commented Apr 25, 2015 at 4:44
  • 3
    type is not returning an object, because you have no objects, only strings. You can't magically regenerate an object, with all of its associated properties and methods, from a string that says <class 'Foo.bar'>. Commented Apr 25, 2015 at 4:46
  • Every file/class is in the same directory. Commented Apr 25, 2015 at 5:01
  • Why not use an automatically parseable serialization format like json, yaml, or pickle? Commented Apr 25, 2015 at 18:41

1 Answer 1

-1

Try the approach from Get python class object from string:

import importlib
...
for i in range(len(data) / 3):    
        # get object data
        cls = data[i * 3]
        x = int(data[i * 3 + 1])
        y = int(data[i * 3 + 2])

        # get module and class
        module_name, class_name = cls.split(".")
        somemodule = importlib.import_module(module_name)

        # instantiate
        obj = getattr(somemodule, class_name)(x, y)

        self.instances.append(obj)

Here is a complete sample (put it in a file named getattrtest.py):

import importlib

class Test1(object):
    def __init__(self, mx, my):
        self.x = mx
        self.y = my

    def printit(self):
        print type(self)
        print self.x, self.y

class Test2(Test1):
    def __init__(self, mx, my):
        # changes x and y parameters...
        self.y = mx
        self.x = my

def main():
    cls = 'getattrtest.Test2'
    # get module and class
    module_name, class_name = cls.split(".")
    somemodule = importlib.import_module(module_name)

    # instantiate
    obj = getattr(somemodule, class_name)(5, 7)
    obj.printit()

if __name__ == "__main__":
    main()

Tested with Python 2.7

Sign up to request clarification or add additional context in comments.

2 Comments

I gave your solution a try, but it appears that the object is not actually being created. I try to call one of the objects' superclass functions, but it's not letting me. How do I create the object after? (BTW thanks for the response.)
@Paolo: I added a complete sample to my answer which shows that it works including function calls to superclass. Maybe there's a problem in oPlayer.oPlayer or CommonObject.Wall

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.