Python NameError when defining class -
the below python fails reason.
import urllib.request, urllib.parse, threading class netvend: def blankcallback(data): pass def setaddress(priv): pass def signcommand(command): pass def sendcommand(command, callback=netvend.blankcallback): return netvend.sendsignedcommand(command, netvend.signcommand(command), callback) def sendsignedcommand(command, sig, callback=netvend.blankcallback): fileobj = urllib.request.urlopen(netvend.commandurl, urllib.parse.urlencode({"address": netvend.eckey.getbitcoinaddress().tostring(), "command": command, "signed": sig})) threading.thread(target=callback, args=(fileobj.read())).start() # act javascript callback. def parseresponce(data): ampsplit = data.split("&") ampfirstcharsplit = ampsplit[0].split("") if ampfirstcharsplit[0] == "s": success = true ampfirstcharsplit.pop(0) commandid = ampfirstcharsplit.join("") returnstring = ampsplit[1] elif ampfirstcharsplit[0] == "e": success = false ampfirstcharsplit.pop(0) errormessage = ampfirstcharsplit.join("") else: raise exception("invalid responce code. contact admin @ 8kstq@notsharingmy.info") return {"success" : success, "commandid" : commandid, "returnstring" : returnstring, "errormessage" : errormessage} def gerneratecommanddata(message): return "data&" + netvend.urlencode(message) def gerneratecommandtip(userid, usats, dataid): return "tip&" + netvend.urlencode(userid) + "&" + netvend.urlencode(usats) + "&" + netvend.urlencode(dataid) def commanddata(message, callback=netvend.blankcallback): return netvend.sendcommand(netvend.gerneratecommanddata(message), callback) def commandtip(userid, usats, dataid, callback=netvend.blankcallback): return netvend.sendcommand(gerneratecommandtip(userid, usats, dataid), callback) def commandtipandmessage(userid, usats, message, callback=netvend.blankcallback): pass netvend.commandurl = "http://ec2-54-213-176-154.us-west-2.compute.amazonaws.com/command.php" netvend.commanddata("test")
i following error:
traceback (most recent call last): file "c:\users\harborow\desktop\raw.py", line 17, in <module> class netvend: file "c:\users\harborow\desktop\raw.py", line 27, in netvend def sendcommand(command, callback=netvend.blankcallback): nameerror: name 'netvend' not defined
you cannot refer class name while still defining it.
the class body executed local namespace; can refer functions , attributes local names instead.
moreover, default values function keyword parameters bound @ definition time, not when method called. use none
sentinel instead.
instead of:
def sendcommand(command, callback=netvend.blankcallback): return netvend.sendsignedcommand(command, netvend.signcommand(command), callback)
use:
def sendcommand(command, callback=none): if callback none: callback = netvend.blankcallback return netvend.sendsignedcommand(command, netvend.signcommand(command), callback)
you wanted use class factory instances instead of namespace functions. if used 1 instance (a singleton) there benefits in creating instance first.
Comments
Post a Comment