Checking whether an object has been created.

Quinn Dunkan quinn at zloty.ugcs.caltech.edu
Tue Oct 17 16:10:39 EDT 2000


On Tue, 17 Oct 2000 18:52:12 +0200, Olivier CARRERE <olivier at vibes.net> wrote:
>Hello,
>
>has anyone a clean method to check if an object has been instanciated?
>
>What I wish to do is to create an object if it hasn't been made before,
>ie something like this :
>
>if not exists(objectReference):
>	objectReference=Object()
># perform stuff on objectReference
>
>Thanks in advance,
>
>- Olivier

try:
    obj
except NameError:
    obj = Obj()

If you have an object which may or may not exist, it would probably be cleaner
to keep it in a dictionary.  Then you could write:

obj = objstore.get('myobj', Obj())

or

obj = objstore.setdefault('myobj', Obj()) # in 2.0

obj = objstore['myobj'] = objstore.get('myobj', Obj()) # in 1.5.2



BTW, calling things xReference or yReference in python is going to get tiring
because *everything* is a reference :)



More information about the Python-list mailing list