{"id":1611,"date":"2011-05-25T10:17:51","date_gmt":"2011-05-25T09:17:51","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1611"},"modified":"2011-05-25T10:17:51","modified_gmt":"2011-05-25T09:17:51","slug":"classes","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2011\/05\/25\/classes\/","title":{"rendered":"Classes"},"content":{"rendered":"<p>Since Python is a object oriented programming language then you can use things like classes to define a object, a class is a bunch of methods that act on the data that is stored within the class itself.  You can store data within the class, but unlike c++\/java\/c# etc, there is no private\/public\/protected, everything is public accessible.<\/p>\n<p>In the example below, the class constructor is called (__init__ method) and the first parameter is the class object itself, which is why it is there and for many reasons why it is called self as a variable name.<\/p>\n<pre lang=\"python\">\r\n\r\n#  self means the object itself.\r\n\r\nclass mysimpleclass :\r\n        __myvar = 0\r\n        def __init__(self,value = 0) :\r\n                self.__myvar = value\r\n\r\n        def printValue(self) :\r\n                print self.__myvar\r\n\r\nsimple = mysimpleclass(5)\r\nsimple.printValue()\r\n<\/pre>\n<p>the printValue method just does that, it will print the value that is sorted within the class object and here is the output<\/p>\n<pre lang=\"bash\">\r\n5\r\n<\/pre>\n<p> if you did not put in the self in the example above for printing out the class object variable __myvar as below<\/p>\n<pre lang=\"python\">\r\nclass mysimpleclass ....\r\n       ....\r\n       def printValue() :\r\n            print __myvar\r\n<\/pre>\n<p>the error would be<\/p>\n<pre lang=\"bash\">\r\nNameError: global name '_mysimpleclass__myvar' is not defined\r\n<\/pre>\n<p>because the interrupter is looking for a global variable and not a variable attached to that object.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since Python is a object oriented programming language then you can use things like classes to define a object, a class is a bunch of methods that act on the data that is stored within the class itself. You can store data within the class, but unlike c++\/java\/c# etc, there is no private\/public\/protected, everything is &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2011\/05\/25\/classes\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Classes<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[286],"tags":[347],"class_list":["post-1611","post","type-post","status-publish","format-standard","hentry","category-python","tag-classes"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1611"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1611\/revisions"}],"predecessor-version":[{"id":1680,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1611\/revisions\/1680"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}