Using 2.6 right now but I assume classes work the same way! All you forgot was to add
self. before the variable, which tells the class that this is a member. However, using global variables like that is impractical. Throw it in the __init__ function - this function is the constructor of Python classes. /biggrin.png' class='bbc_emoticon' alt=':biggrin:' />
Spoiler class netinfo: def __init__(self): self.addr = '' self.netmask = '' self.netaddr = '' self.fhost = '' self.lhost = '' self.bcast = '' self.hostcount = 0 self.netcount = 0 def changeAddr(self): self.addr = "123.456.789" def getAddr(self): print self.addrif __name__ == '__main__': n = netinfo() n.changeAddr() n.getAddr()
In other news, I figured out the shortest - likely cross platform - way to grab global keyboard input in Python! This program only looks for the Escape key to be pressed, I'm threading something similar to mark the end of a program, but I included a way to test for more than just the Escape key.
Spoiler # captures keyboard events, prints something out if escape is pressedfrom Xlib.display import Displayfrom Xlib import Xdef handle(mEvent): keycode = mEvent.detail if mEvent.type is X.KeyPress: print keycodedef keyboardHook(): #get current display display = Display() root = display.screen().root # subscribe to keypress events root.change_attributes(event_mask = X.KeyPressMask) # subscribe to escape key root.grab_key(9, X.AnyModifier, 1, X.GrabModeAsync, X.GrabModeAsync) # multiple keys can be subscribed to like so: # for i in keylist: #keylist containing your list of desired keys' keycodes # root.grab_key(i, X.AnyModifier, 1, X.GrabModeAsync, X.GrabModeAsync) while True: event = root.display.next_event() handle(event)if __name__ == '__main__': keyboardHook()
Heavily based off a script I found, and influenced by pyxhook. Hope someone finds it useful!