{"id":215,"date":"2019-12-26T23:21:00","date_gmt":"2019-12-26T23:21:00","guid":{"rendered":""},"modified":"2024-09-13T15:04:52","modified_gmt":"2024-09-13T15:04:52","slug":"python-tutorial-series-classes","status":"publish","type":"post","link":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-classes\/","title":{"rendered":"Python Tutorial Series \u2013 Classes"},"content":{"rendered":"<p>In object oriented programming, classes are used as a blueprint to define objects in an application. Classes are a way to organize code, provide the ability to easily extend functionality and allow for reuse.<\/p>\n<p>In Python, a class is defined as follows:<\/p>\n<pre><\/pre>\n<p>\nclass BaseClass:<br \/>\n&nbsp; # constructor<br \/>\n&nbsp; def __init__(self, param1):<br \/>\n&nbsp; &nbsp; self.param1 = param1<br \/>\n&nbsp; &nbsp; self._aprotectedvariable = 1<br \/>\n&nbsp; &nbsp; self.__aprivatevariable = 2<br \/>\n&nbsp;<br \/>\n&nbsp; def set_param1(self, param1):<br \/>\n&nbsp; &nbsp; self.param1 = param1<\/p>\n<p>&nbsp; def get_param1(self):<br \/>\n&nbsp; &nbsp; return self.param1<\/p>\n<p>&nbsp; def who_am_i(self):<br \/>\n&nbsp; &nbsp; print(&#8216;I am a Base Class&#8217;)<\/p>\n<p>&nbsp; def _a_protected_method(self):<br \/>\n&nbsp; &nbsp; print(&#8216;I am a protected method&#8217;)<br \/>\n&nbsp;<br \/>\n&nbsp; def __a_private_method(self):<br \/>\n&nbsp; &nbsp; print(&#8216;I am a private method&#8217;)<\/p>\n<p>&nbsp; private_method = __a_private_method<\/p>\n<p>\nClass names are camel cased, which means each word starts with a capital letter and there are no underscores.<\/p>\n<p>\n<strong>Variable\/Method Scope:<\/strong><\/p>\n<p>Protected variables and methods are defined by adding one underscore to the beginning of the variable or method name. Protected variables and methods are still accessible outside of the class. However, a best practice is not to access either one and to use caution if it really needs to be done.<br \/>\nPrivate variables and methods are defined by adding two underscores to the beginning of the variable name. Private variable and method names are mangled, so they are not as easy to access outside of the class. It can still be done by calling a variable as _class__variable, ex: _BaseClass__aprivatevariable. This should never really be done though.<br \/>\nA class may also provide access to private or protected members by assigning a public variable to them, which is what the line:<\/p>\n<pre><\/pre>\n<p>\nprivate_method = __a_private_method<\/p>\n<p>\nis doing.<\/p>\n<p>\n<strong>Inheritance:<\/strong><\/p>\n<p>A child class can be created as follows:<\/p>\n<pre><\/pre>\n<p>\nclass ChildClassA(BaseClass):<br \/>\n&nbsp; def __init__(self, parama, paramb):<br \/>\n&nbsp; &nbsp; super().__init__(parama)<br \/>\n&nbsp; &nbsp; self.param2 = paramb<br \/>\n&nbsp;<br \/>\n&nbsp; def who_am_i(self):<br \/>\n&nbsp; &nbsp; print(&#8216;I am a Child Class A&#8217;)<\/p>\n<p>\nThe who_am_i method demonstrates how a child class can override a method in a parent class.<\/p>\n<p>\n<strong>Example Usage:<\/strong><\/p>\n<p>A class is instantiated as follows:<\/p>\n<pre><\/pre>\n<p>\nbase_class = BaseClass(&#8216;abc&#8217;)<br \/>\nbase_class.who_am_i()<br \/>\nbase_class.private_method()<br \/>\nbase_class._BaseClass__a_private_method()<br \/>\nprint(base_class._BaseClass__aprivatevariable)<\/p>\n<p>child_class_a = ChildClassA(&#8216;123&#8217;, &#8216;456&#8217;)<br \/>\nchild_class_a.who_am_i()<\/p>\n<p>\n<a href=\"https:\/\/amzn.to\/2EimtFd\" rel=\"noopener\" target=\"_blank\">More Python<\/a><\/p>\n<p><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=B0785Q7GSY&amp;asins=B0785Q7GSY&amp;linkId=db5e82d74b668701871debd783c9ddf6&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=B077Z55G3B&amp;asins=B077Z55G3B&amp;linkId=52c196ba1fda10ea7b99169422b21af7&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=1491912057&amp;asins=1491912057&amp;linkId=e818186a630b59613fa23378eab853f9&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In object oriented programming, classes are used as a blueprint to define objects in an application. Classes are a way to organize code, provide the ability to easily extend functionality and allow for reuse. In Python, a class is defined as follows: class BaseClass: &nbsp; # constructor &nbsp; def __init__(self, param1): &nbsp; &nbsp; self.param1 = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13,343,341],"tags":[],"class_list":["post-215","post","type-post","status-publish","format-standard","hentry","category-python","category-python-tutorials","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Tutorial Series \u2013 Classes - Zofxare Blog Home<\/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:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-classes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Tutorial Series \u2013 Classes - Zofxare Blog Home\" \/>\n<meta property=\"og:description\" content=\"In object oriented programming, classes are used as a blueprint to define objects in an application. Classes are a way to organize code, provide the ability to easily extend functionality and allow for reuse. In Python, a class is defined as follows: class BaseClass: &nbsp; # constructor &nbsp; def __init__(self, param1): &nbsp; &nbsp; self.param1 = [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-classes\/\" \/>\n<meta property=\"og:site_name\" content=\"Zofxare Blog Home\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-26T23:21:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-13T15:04:52+00:00\" \/>\n<meta name=\"author\" content=\"adminuser\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"adminuser\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-classes\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-classes\\\/\"},\"author\":{\"name\":\"adminuser\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"headline\":\"Python Tutorial Series \u2013 Classes\",\"datePublished\":\"2019-12-26T23:21:00+00:00\",\"dateModified\":\"2024-09-13T15:04:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-classes\\\/\"},\"wordCount\":407,\"commentCount\":0,\"articleSection\":[\"python\",\"python tutorials\",\"tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-classes\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-classes\\\/\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-classes\\\/\",\"name\":\"Python Tutorial Series \u2013 Classes - Zofxare Blog Home\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\"},\"datePublished\":\"2019-12-26T23:21:00+00:00\",\"dateModified\":\"2024-09-13T15:04:52+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-classes\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-classes\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-classes\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Tutorial Series \u2013 Classes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\",\"name\":\"Zofxare Blog Home\",\"description\":\"The Zofxare blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\",\"name\":\"adminuser\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"caption\":\"adminuser\"},\"sameAs\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\"],\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/author\\\/adminuser\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Tutorial Series \u2013 Classes - Zofxare Blog Home","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:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-classes\/","og_locale":"en_US","og_type":"article","og_title":"Python Tutorial Series \u2013 Classes - Zofxare Blog Home","og_description":"In object oriented programming, classes are used as a blueprint to define objects in an application. Classes are a way to organize code, provide the ability to easily extend functionality and allow for reuse. In Python, a class is defined as follows: class BaseClass: &nbsp; # constructor &nbsp; def __init__(self, param1): &nbsp; &nbsp; self.param1 = [&hellip;]","og_url":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-classes\/","og_site_name":"Zofxare Blog Home","article_published_time":"2019-12-26T23:21:00+00:00","article_modified_time":"2024-09-13T15:04:52+00:00","author":"adminuser","twitter_card":"summary_large_image","twitter_misc":{"Written by":"adminuser","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-classes\/#article","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-classes\/"},"author":{"name":"adminuser","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"headline":"Python Tutorial Series \u2013 Classes","datePublished":"2019-12-26T23:21:00+00:00","dateModified":"2024-09-13T15:04:52+00:00","mainEntityOfPage":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-classes\/"},"wordCount":407,"commentCount":0,"articleSection":["python","python tutorials","tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-classes\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-classes\/","url":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-classes\/","name":"Python Tutorial Series \u2013 Classes - Zofxare Blog Home","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website"},"datePublished":"2019-12-26T23:21:00+00:00","dateModified":"2024-09-13T15:04:52+00:00","author":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"breadcrumb":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-classes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-classes\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-classes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zofxare.com\/zofxare\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Tutorial Series \u2013 Classes"}]},{"@type":"WebSite","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website","url":"https:\/\/zofxare.com\/zofxare\/blog\/","name":"Zofxare Blog Home","description":"The Zofxare blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/zofxare.com\/zofxare\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8","name":"adminuser","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","caption":"adminuser"},"sameAs":["https:\/\/zofxare.com\/zofxare\/blog"],"url":"https:\/\/zofxare.com\/zofxare\/blog\/author\/adminuser\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":1,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":248,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/215\/revisions\/248"}],"wp:attachment":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}