{"id":967,"date":"2019-05-17T14:51:11","date_gmt":"2019-05-17T14:51:11","guid":{"rendered":"http:\/\/tech.avant.net\/q\/?p=967"},"modified":"2019-05-17T14:56:07","modified_gmt":"2019-05-17T14:56:07","slug":"using-getattr-in-python","status":"publish","type":"post","link":"https:\/\/tech.avant.net\/q\/using-getattr-in-python\/","title":{"rendered":"Using getattr in Python"},"content":{"rendered":"<p>I would like to execute a named function on a python object by variable name. For example, let&#8217;s say I&#8217;m reading in input that looks something like this:<\/p>\n<pre class=\"sh\">enqueue 1\nenqueue 12\nenqueue 5\nenqueue 9\nsort\nreverse\ndequeue\nprint<\/pre>\n\n\n<p>Afterwards, we should see:<\/p>\n\n\n<pre class=\"sh\">[9,5,1]<\/pre>\n\n\n<p>Let&#8217;s say we need to implement a data structure that consumes this input. Fortunately, all of this behavior already exists within the built-in <em>list<\/em> datatype. What we can do is extend the built-in <em>list<\/em> to map the appropriate methods, like so:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class qlist(list):\n    def enqueue(self, v):\n        self.insert(0,v)\n\n    def dequeue(self):\n        return self.pop()\n\n    def print(self):\n        print(self)<\/pre>\n\n\n\n<p>The <em>sort<\/em> and <em>reverse<\/em> methods are already built-in to <em>list<\/em>, so we don&#8217;t need to map them. Now, we simply need a driver program that reads and processes commands to our new qlist class. Rather than map out the different commands in if\/else blocks, or use eval(), we can simply use getattr, for example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if __name__ == '__main__':\n    thelist = qlist()\n    while line in sys.stdin:\n        cmd = line.split()\n        params = (int(x) for x in cmd[1:])\n        getattr(thelist, cmd[0])(*params)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I would like to execute a named function on a python object by variable name. For example, let&#8217;s say I&#8217;m reading in input that looks something like this: enqueue 1 enqueue 12 enqueue 5 enqueue 9 sort reverse dequeue print Afterwards, we should see: [9,5,1] Let&#8217;s say we need to implement a data structure that [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"_links":{"self":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/967"}],"collection":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/comments?post=967"}],"version-history":[{"count":3,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/967\/revisions"}],"predecessor-version":[{"id":970,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/967\/revisions\/970"}],"wp:attachment":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/media?parent=967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/categories?post=967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/tags?post=967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}