Python gotcha – Forgetting the parentheses after a method name

Technically, I suppose this is not really a gotcha. But for someone like me who has to switch between scripting languages frequently, it’s a potential “gotcha”.

In Python, functions and methods are legitimate objects. That’s why you can do this:

App = Application
LogMsg = App.LogMessage
ClassName = App.ClassName

but sometimes you might forget the parentheses after a name, and unlike JScript, you won’t get a syntax error (at least not for the missing parentheses). For example, this little snippet will give you a “# AttributeError: ‘NoneType’ object has no attribute ‘AddKey'” error:

param = Application.Dictionary.GetObject( "null.kine.local.rotx" )

if str(param.Source) == 'None':
    fcurve = param.Source
    fcurve.AddKey( 10, 90 )
    fcurve.AddKey( 20, 45 )

In JScript, you’d get a syntax error for the missing parentheses. I’m not saying that that is better 😉 it’s just a difference you have to be aware of.

param = Dictionary.GetObject( "null.kine.local.rotx" )
// ERROR : Object doesn't support this property or method - [line 3]

Some related links:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s