using a class from a module

hy guys ! i made a class and put it in a module, and i’m trying to instance objects of that class inside a script in blender with no succes so far,
for example i have my module objects.py :


class Car :
     tokenVariable = "i'm being used to make a point"

the script where i’m trying to use it :


import objects
BMW = objects.Car() # BMW is an instance of the Car class
print "what's your purpose in life ?"
print BMW.tokenVariable

the interpreter is telling me that the module objects has no atribute Car
can anyone tell me how to do this right ? (how to instance objects of a class from a module inside a script)

You forgot the parenthesis.
It should be:

class Car():
    tokenVariable = "i'm being used to make a point"

No, you don’t need the parenthesis in the class definition; you’re thinking of instantiation.

@rareseu

Believe it or not, it’s the fact that you’ve named your module “objects”…or at least, that’s the conclusion I arrived upon based on my tests.

Change the module name to something else (something less common), and your code should execute properly.

*Edit

Hmm, I just tried it again, and it worked despite the fact that the module was named “objects”… this may be a new anomaly.

In short: I don’t know.

No, you don’t need the parenthesis in the class definition; you’re thinking of instantiation.

Oh really? I’ve always done it that way, ruby habits. Thanks for pointing that out.

aww man just my luck, thanks for helping out guys

I found that when you import a custom script, and then make changes to that script and run the game again, the importet script is not updated. To do that, you can put “reload(objects)” under your import line to make it update, but only for development. I’d take it out again when your game is done.

Maybe it helps you, this is how it should look:

import objects
reload(objects)

Oh, and I wouldn’t put a space between the class name and the colon in the definition. I don’t know if it’s allowed or not, but just to be safe.