9

Before you down rep this post, it hasn't been asked anywhere that I can find.

I'm checking for the existence of a list by using

if 'self.locList' in locals():
    print 'it exists'

But it doesn't work. It never thinks it exists. This must be because I'm using inheritance and the self. to reference it elsewhere, I don't understand what's happening.

Can anyone shed some light please?

Here is the full code:

import maya.cmds as cmds

class primWingS():
    def __init__(self):
        pass
    def setupWing(self, *args):
        pass
    def createLocs(self, list):
        for i in range(list):
    if 'self.locList' in locals():
        print 'it exists'
            else:
                self.locList = []
            loc = cmds.spaceLocator(n = self.lName('dummyLocator' + str(i + 1) + '_LOC'))
            self.locList.append(loc)
            print self.locList


p = primWingS()
Vii
  • 813
  • 3
  • 20
  • 32
  • 1
    Can you check the indentation of your code? I think I can tell how it should be but it's hard to tell. – Marius Feb 25 '13 at 02:31
  • 2
    Why aren't you creating it inside `__init__`, rather than checking every time? – wim Feb 25 '13 at 02:32

3 Answers3

22

I think you want hasattr(self,'locList')

Although, you're usually better off trying to use an attribute and catching the AttributeError which gets raised if it isn't present:

try:
    print self.locList
except AttributeError:
    self.locList = "Initialized value"
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • 3
    +1: For the [Python EAFP idiom](http://docs.python.org/2/glossary.html#term-eafp). – johnsyweb Feb 25 '13 at 02:33
  • @Johnsyweb -- yeah, from a performance standpoint, `try`-`except` will almost certainly beat `hasattr` since the docs explicitly state that `hasattr` is implemented by trying `getattr` and checking for exceptions ... – mgilson Feb 25 '13 at 02:47
10

Answering from a bit of a different perspective. Try ... catch, getattr or dir is the way to go if you just want the code to work.

The call locals() returns a dictionary of local scope. That is it includes self. However, you are asking for the child of self (self.locList). The child is simply not in the dictionary. The closest thing to what you are doing would be:

if 'locList' in dir(self):
    print 'it exists'

function dir being the generic way to query items of objects. But as noted in the other posts, it does not make much sense to query objects' attributes from a speed standpoint.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
joojaa
  • 4,354
  • 1
  • 27
  • 45
1

You can use try/except or getattr with a default value, but those things don't make sense with your code. The __init__ method is there to initialize the object:

def __init__(self):
    self.locList = []

It makes no sense to allow locList not to exist. A zero length list is an object without locations.

hdante
  • 7,685
  • 3
  • 31
  • 36
  • putting it in the init doesn't work, it doesn't recognize there being a list at all – Vii Feb 25 '13 at 02:55
  • 2
    This is not correct, the init method has as the only goal to setup class attributes. – hdante Feb 25 '13 at 03:09
  • It looks like the broken indentation is not only in the pasted code. Rewrite the createLocs method with correct indentation, or python will refuse to do what you want. – hdante Feb 25 '13 at 03:15