Patrick van der Willik wrote:
Ok, I'm confused here. I'm very new to Python aswell, so this probably doesn't help very much.

I think this is a big part of your issue. IronPython is a faithful implementation of Python and what you are tripping over are the Python scoping rules and the way modules behave.

Classes and functions in modules only have access to the names defined in their scope *or* names imported into that scope (every module has an associated ScriptScope which is the module namespace).

As Dino says - the correct way to make names available to a module is for that module to import them. If you publish objects into the runtime Globals then any scope can import them.

As oppossed to trying to constanly import a huge set of require variables all the time, I thought it might be a good idea to introduce 1 object into the globals space which will host a variety of the functions.

There is no global space in Python.

Instead put all your functions / objects into a single module published into the runtime globals. Your modules can then do:

   import server

   server.function1()
   server.function2()

Alternatively you can do "from something import *" but this is generally seen as bad form in Python as it causes namespace pollution and makes it harder to see where the names used in your code come from.

Michael

I did this by calling engine.Runtime.Globals.SetVariable("server", server); then loading source script, compile it and invoke it on the scope.
def OnConnect(user):
   global server
   server.log.Write("Test")

However, this results in the same error, where server is not defined. I feel kinda silly here, as to me is would seem rather logical that when I introduce variables into a ScriptScope and then execute a function within that scope that it would work. I understand that variables themselfs are limited to a module, which sounds logical, but it feels weird that even tho I inject a variable directly into the globals table or into the scope that I created before executing a script on it, I can't (in)directly use them from script.

The other solution would be to have the server object be part of every function call to scripts, but that sounds kinda silly...

Patrick

Dino Viehland wrote:
You can set the variables in ScriptRuntime.Globals but then the user
will need to import them from Globals to have access to them. There's no
way to automatically expose globals to every script though.

_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


--
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog


_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to