Testing if a global var exists

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
miljbee
Experienced User
Posts: 146
Joined: Fri Mar 27, 2009 1:29 pm
Location: Orléans, France

Testing if a global var exists

Post by miljbee » Tue Oct 12, 2010 9:54 am

Hello,

In a python script, I would like to test if a global var exists.

Something like :

if eg.globals.somevar exists:
eg.TriggerEvent(eg.globals.somevar)
else:
eg.globals.somevar="default"

Can you tell me how to write the bold line ?

Thanks !
miljbee
TCP Events : A Better Network Event Sender/Receiver Plugin.
The Network Event Sender/Receiver in C#
Get events in EG from Google Calendar.

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Testing if a global var exists

Post by Pako » Tue Oct 12, 2010 11:48 am

Code: Select all

if "somevar" in eg.globals.__dict__:
    eg.TriggerEvent(eg.globals.somevar)
else:
    eg.globals.somevar="default"
Pako

miljbee
Experienced User
Posts: 146
Joined: Fri Mar 27, 2009 1:29 pm
Location: Orléans, France

Re: Testing if a global var exists

Post by miljbee » Tue Oct 12, 2010 5:00 pm

thank you !
miljbee
TCP Events : A Better Network Event Sender/Receiver Plugin.
The Network Event Sender/Receiver in C#
Get events in EG from Google Calendar.

kalia
Experienced User
Posts: 109
Joined: Wed Aug 12, 2009 1:10 am

Re: Testing if a global var exists

Post by kalia » Thu Oct 14, 2010 5:11 am

Thanks Pako.

I always wondered about this also, but never asked.

Post Reply