0

So I am pretty new to python so be kind. I am trying to create a global sort of object directory as a base for all my programs as a means to keep track of all created object instances in my programs.

so..

I create a class that holds a list of object references ( not id's ). example:

class objDirectory:
    name = ""
    count = 0
    objDir = []
    def __init__( self ):
        print( "Initiating object directory." )
        name = "objDirectory"
        self.count= 1
        self.objDir = [ self ]
        return
    def getObj( self, name ):
        print( "Searching directory for:", name )
        for o in self.objDir:
            if o.name is name:
                print( "Returning:", name )
                return obj
            else:
                print( "Search failed" )
            return

But, once an object is added to a list and I run the get script it does not return my object. I even verify by using directory.objDir[x]. ( this always references my object) .

What am I not doing, or doing wrong?

Thanks.

Results:

setDirectory() Initiating object directory. Global directory reference set test = obj( "test" ) Initiating: test Duplicate check Logging: test1 Object count: 2 t1 = directory.getObj( "test1" ) Searching directory for: test1 Search failed print( directory.objDir ) [<main.objDirectory object at 0x032C86F0>, <main.obj object at 0x032C8710>]

1 Answers1

0

I don't quite understand how you plan to use this object, however I find that there are a few problems with the __init__ method.
First of all, I guess that your object should be a singleton, however it is not. You define the class attributes here:

class objDirectory:
    name = ""
    count = 0
    objDir = []

But in your __init__ method you always override them, instead of adding to them. This means that every time you will write something like a=objDirectory(), count will be set to 1 for that instance. Also, notice that (at least in python2.7), you'll need a @classmethod decorator for every function you wish be able to modify class attributes (as opposed to the instance's attributes). Like so:

class objDirectory:
    count=0

    @classmethod
    def add_object(cls):
        cls.count+=1
    def __init__(self):
        self.add_object()

This goes for objDir as well.
Second, in your __init__ method, you have a variable called name, which is never used. Perhaps you meant self.name?

There are better ways to implement singletons, however here is a simple example that might be easier to understand if you're new to python:

class mySingleton(object):
    singletonObject = None
    def __new__(cls):
        if not cls.singletonObject:
            cls.singletonObject = super(mySingleton, cls).__new__(cls)
        return cls.singletonObject

The __new__ method is a static method which creates the actual instance. The return value is the new instance created. Overriding it like so will let me get the same instance for each call to mySingleton(); the statement mySingleton() is mySingleton() will always be true.

micromoses
  • 6,747
  • 2
  • 20
  • 29
  • Thank you so much I need to wrap my head around some of the things you have here but, I still have alot to learn. – user3312001 Feb 18 '14 at 15:46