{"id":124,"date":"2011-06-28T08:41:35","date_gmt":"2011-06-28T08:41:35","guid":{"rendered":"http:\/\/thewiredguy.com\/wordpress\/?p=124"},"modified":"2011-06-28T08:41:35","modified_gmt":"2011-06-28T08:41:35","slug":"classes-in-python","status":"publish","type":"post","link":"https:\/\/inullable.in\/blog\/?p=124","title":{"rendered":"Classes in Python"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"alignnone\" src=\"http:\/\/www.python.org\/images\/python-logo.gif\" alt=\"\" width=\"180\" height=\"49\" \/><a href=\"http:\/\/www.python.org\">Python<\/a> is a super-duper cool language, I like it&#8230;everybody loves it. It is also popular as the <a title=\"hacker language\" href=\"http:\/\/oss.coresecurity.com\/\" target=\"_blank\">|-|4x0r5<\/a> language. It is dynamic and has a clear syntax, most of all fun to use.<\/p>\n<p>But so many books and manuals underplay classes in python that it mostly lives in early programmers as plain scripting language having support for procedures\/methods. In reality, Python supports object-oriented programming with classes and multiple inheritance.<\/p>\n<p>Here is an approximate syntactical comparison beween class declaration in C# and python:<\/p>\n<p><strong>C# Version:<\/strong><\/p>\n<pre>public class Person\n{\n\tint Age;\n\tpublic string Name;\n\tfloat Height;\n\tfloat Weight;\n\n\tpublic Person(int age,float height,float weight)\n\t{\n\t\tAge = age;\n\t\tHeight = height;\n\t\tWeight = weight;\n\t\tName = \"No Name\"\n\t}\n\n\tpublic void GetInfo()\n\t{\n\t\tsystem.console.WriteLine(\"Name: {0}\",Name);\n\t\tsystem.console.WriteLine(\"Weight: {0}\",Weight);\n\t\tsystem.console.WriteLine(\"Height: {0}\",Height);\n\t\tsystem.console.WriteLine(\"Age: {0}\",Age);\n\t}\n}<\/pre>\n<p><strong>Python Version<\/strong><\/p>\n<pre>class Person:\n\tName = \"No Name\"\n\tdef __init__(self,age,height,weight):\n\t\tself.Age = age\n\t\tself.Height = height\n\t\tself.Weight = weight\n\n\tdef GetInfo(self):\n\t\tprint \"Name: \", self.Name\n\t\tprint \"Weight: \", self.Weight\n\t\tprint \"Height: \", self.Height\n\t\tprint \"Age: \", self.Age\n\n------------------------------------------------------------------------------------\nUsage:\n\np = Person(20,5.11,70)\np.GetInfo()\nprint(\"nn\")\np.Name = \"Chuck Norris\"\np.GetInfo()\n\n------------------------------------------------------------------------------------\nOutput:\nName:  No Name\nWeight:  70\nHeight:  5.11\nAge:  20\n\nName:  Chuck Norris\nWeight:  70\nHeight:  5.11\nAge:  20<\/pre>\n<p>Learn more about classes and objects oriented programming in python <a title=\"here\" href=\"http:\/\/www.freenetpages.co.uk\/hp\/alan.gauld\/tutclass.htm\" target=\"_blank\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python is a super-duper cool language, I like it&#8230;everybody loves it. It is also popular as the |-|4x0r5 language. It is dynamic and has a clear syntax, most of all fun to use. But so many books and manuals underplay classes in python that it mostly lives in early programmers as plain scripting language having [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[231],"tags":[],"_links":{"self":[{"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=\/wp\/v2\/posts\/124"}],"collection":[{"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=124"}],"version-history":[{"count":0,"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=\/wp\/v2\/posts\/124\/revisions"}],"wp:attachment":[{"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/inullable.in\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}