{"id":210,"date":"2021-08-04T13:48:23","date_gmt":"2021-08-04T13:48:23","guid":{"rendered":"https:\/\/www.jibsheet.net\/linux\/?p=210"},"modified":"2021-08-04T16:51:19","modified_gmt":"2021-08-04T16:51:19","slug":"python-how-to-reference-class-functions-in-class-variable","status":"publish","type":"post","link":"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/","title":{"rendered":"Python How to reference class functions in class variable."},"content":{"rendered":"<p>In static languages &#8211; it makes since to put the class variable at the top of the Class.\u00a0 However, since Python is dynamic, it can&#8217;t know about the functions until it processes them.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-lineoffset=\"2\">class AwsProcessor:\r\n    service_dict = {'s3': process_s3, 'ebs': process_ebs}\r\n    def __init__(self, processing_service):\r\n        self.service = processing_service\r\n    def process_ebs(self):\r\n        pass\r\n    def process_s3(self):\r\n        pass<\/pre>\n<p>This gives error<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">  File \"c:\\aws-scripts\\class AwsProcessor.py\", line 1, in &lt;module&gt;\r\n    class AwsProcessor:\r\n  File \"c:\\aws-scripts\\class AwsProcessor.py\", line 2, in AwsProcessor\r\n    service_dict = {'s3': process_s3, 'ebs': process_ebs}\r\nNameError: name 'process_s3' is not defined<\/pre>\n<p>If you move the variable below the function def it will run without error<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-highlight=\"8\">class AwsProcessor:\r\n    def __init__(self, processing_service):\r\n        self.service = processing_service\r\n    def process_ebs(self):\r\n        pass\r\n    def process_s3(self):\r\n        pass\r\n    service_dict = {'s3': process_s3, 'ebs': process_ebs}<\/pre>\n<p>to call such a function &#8211; you need to reference the class namespace.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def process_services(self):\r\n       AwsProcessor.service_dict[self.service](self)<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In static languages &#8211; it makes since to put the class variable at the top of the Class.\u00a0 However, since Python is dynamic, it can&#8217;t know about the functions until it processes them. class AwsProcessor: service_dict = {&#8216;s3&#8217;: process_s3, &#8216;ebs&#8217;: process_ebs} def __init__(self, processing_service): self.service = processing_service def process_ebs(self): pass def process_s3(self): pass This gives error File &#8220;c:\\aws-scripts\\class AwsProcessor.py&#8221;, line [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[7],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python How to reference class functions in class variable. - Jon Allen&#039;s Linux Stuff<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python How to reference class functions in class variable. - Jon Allen&#039;s Linux Stuff\" \/>\n<meta property=\"og:description\" content=\"In static languages &#8211; it makes since to put the class variable at the top of the Class.\u00a0 However, since Python is dynamic, it can&#8217;t know about the functions until it processes them. class AwsProcessor: service_dict = {&#039;s3&#039;: process_s3, &#039;ebs&#039;: process_ebs} def __init__(self, processing_service): self.service = processing_service def process_ebs(self): pass def process_s3(self): pass This gives error File &quot;c:aws-scriptsclass AwsProcessor.py&quot;, line [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/\" \/>\n<meta property=\"og:site_name\" content=\"Jon Allen&#039;s Linux Stuff\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-04T13:48:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-04T16:51:19+00:00\" \/>\n<meta name=\"author\" content=\"jonallen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"jonallen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/\"},\"author\":{\"name\":\"jonallen\",\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993\"},\"headline\":\"Python How to reference class functions in class variable.\",\"datePublished\":\"2021-08-04T13:48:23+00:00\",\"dateModified\":\"2021-08-04T16:51:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/\"},\"wordCount\":78,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/\",\"url\":\"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/\",\"name\":\"Python How to reference class functions in class variable. - Jon Allen&#039;s Linux Stuff\",\"isPartOf\":{\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#website\"},\"datePublished\":\"2021-08-04T13:48:23+00:00\",\"dateModified\":\"2021-08-04T16:51:19+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.jibsheet.net\/linux\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python How to reference class functions in class variable.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#website\",\"url\":\"https:\/\/www.jibsheet.net\/linux\/\",\"name\":\"Jon Allen's Linux Stuff\",\"description\":\"Howto&#039;s, Observations, and stuff\",\"publisher\":{\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.jibsheet.net\/linux\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993\",\"name\":\"jonallen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7d7c061ce9db8f5e4710555585475882?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7d7c061ce9db8f5e4710555585475882?s=96&d=mm&r=g\",\"caption\":\"jonallen\"},\"logo\":{\"@id\":\"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/www.jibsheet.net\/linux\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python How to reference class functions in class variable. - Jon Allen&#039;s Linux Stuff","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/","og_locale":"en_US","og_type":"article","og_title":"Python How to reference class functions in class variable. - Jon Allen&#039;s Linux Stuff","og_description":"In static languages &#8211; it makes since to put the class variable at the top of the Class.\u00a0 However, since Python is dynamic, it can&#8217;t know about the functions until it processes them. class AwsProcessor: service_dict = {'s3': process_s3, 'ebs': process_ebs} def __init__(self, processing_service): self.service = processing_service def process_ebs(self): pass def process_s3(self): pass This gives error File \"c:aws-scriptsclass AwsProcessor.py\", line [&hellip;]","og_url":"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/","og_site_name":"Jon Allen&#039;s Linux Stuff","article_published_time":"2021-08-04T13:48:23+00:00","article_modified_time":"2021-08-04T16:51:19+00:00","author":"jonallen","twitter_card":"summary_large_image","twitter_misc":{"Written by":"jonallen","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/#article","isPartOf":{"@id":"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/"},"author":{"name":"jonallen","@id":"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993"},"headline":"Python How to reference class functions in class variable.","datePublished":"2021-08-04T13:48:23+00:00","dateModified":"2021-08-04T16:51:19+00:00","mainEntityOfPage":{"@id":"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/"},"wordCount":78,"commentCount":0,"publisher":{"@id":"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/","url":"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/","name":"Python How to reference class functions in class variable. - Jon Allen&#039;s Linux Stuff","isPartOf":{"@id":"https:\/\/www.jibsheet.net\/linux\/#website"},"datePublished":"2021-08-04T13:48:23+00:00","dateModified":"2021-08-04T16:51:19+00:00","breadcrumb":{"@id":"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.jibsheet.net\/linux\/index.php\/2021\/08\/04\/python-how-to-reference-class-functions-in-class-variable\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.jibsheet.net\/linux\/"},{"@type":"ListItem","position":2,"name":"Python How to reference class functions in class variable."}]},{"@type":"WebSite","@id":"https:\/\/www.jibsheet.net\/linux\/#website","url":"https:\/\/www.jibsheet.net\/linux\/","name":"Jon Allen's Linux Stuff","description":"Howto&#039;s, Observations, and stuff","publisher":{"@id":"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.jibsheet.net\/linux\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/295a80d0b2aa929b648c4fdddee83993","name":"jonallen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7d7c061ce9db8f5e4710555585475882?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7d7c061ce9db8f5e4710555585475882?s=96&d=mm&r=g","caption":"jonallen"},"logo":{"@id":"https:\/\/www.jibsheet.net\/linux\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/www.jibsheet.net\/linux"]}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/posts\/210"}],"collection":[{"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/comments?post=210"}],"version-history":[{"count":3,"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/posts\/210\/revisions"}],"predecessor-version":[{"id":213,"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/posts\/210\/revisions\/213"}],"wp:attachment":[{"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/media?parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/categories?post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jibsheet.net\/linux\/index.php\/wp-json\/wp\/v2\/tags?post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}