{"id":233461,"date":"2025-06-13T06:43:12","date_gmt":"2025-06-13T06:43:12","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=233461"},"modified":"2025-06-13T06:43:14","modified_gmt":"2025-06-13T06:43:14","slug":"creating-a-method-object","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/06\/13\/creating-a-method-object\/","title":{"rendered":"Creating a method object"},"content":{"rendered":"\n<p> Creating a method object. Define the method object inc_num_kids() for PersonInfo. inc_num_kids increments the member data num_kids. Sample output for the given program with one call to inc_num_kids(): Kids: 0 New baby, kids now: 1 1 class PersonInfo: 2 3 def <strong>init<\/strong>(self): 4 self.num_kids = 0 5 6 # FIXME: Write inc_num_kids(self) 7 8 person1 = PersonInfo() 9 10 print(&#8216;Kids:&#8217;, person1.num_kids) 11 person1.inc_num_kids() 12 print(&#8216;New baby, kids now:&#8217;, person1.num_kids)<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-1-color\">The Correct Answer and Explanation is:<\/mark><\/strong><\/p>\n\n\n\n<p>ere is the <strong>correct version<\/strong> of the program, including the <code>inc_num_kids()<\/code> method and a corrected constructor:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopyEdit<code>class PersonInfo:\n    def __init__(self):\n        self.num_kids = 0\n\n    def inc_num_kids(self):\n        self.num_kids += 1\n\nperson1 = PersonInfo()\n\nprint('Kids:', person1.num_kids)\nperson1.inc_num_kids()\nprint('New baby, kids now:', person1.num_kids)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation (Book Style \u2013 ~300 Words):<\/h3>\n\n\n\n<p>In object-oriented programming (OOP), a <strong>method<\/strong> is a function that belongs to a class. It operates on data that belongs to the object created from that class. In the given program, we are working with a class called <code>PersonInfo<\/code>. This class models a person\u2019s information and currently keeps track of the number of children (kids) a person has.<\/p>\n\n\n\n<p>First, the special method <code>__init__()<\/code> is used to initialize a new object of the class. It is known as the <strong>constructor<\/strong> and is called automatically when an object is created. Inside <code>__init__()<\/code>, we define and initialize the data member <code>num_kids<\/code> to 0, meaning that a new person starts with zero kids.<\/p>\n\n\n\n<p>The next step is to define a method called <code>inc_num_kids()<\/code>. This method will increase the <code>num_kids<\/code> attribute by 1 every time it is called. The keyword <code>self<\/code> is used within the class to refer to the current object. Therefore, <code>self.num_kids += 1<\/code> means: increase the number of kids for the current object by 1.<\/p>\n\n\n\n<p>In the main part of the program, we create an instance of the <code>PersonInfo<\/code> class using <code>person1 = PersonInfo()<\/code>. This calls the constructor and sets <code>person1.num_kids<\/code> to 0. The first <code>print()<\/code> statement confirms this by outputting \u201cKids: 0\u201d.<\/p>\n\n\n\n<p>Next, we call the method <code>person1.inc_num_kids()<\/code>. This increases <code>person1.num_kids<\/code> from 0 to 1. The second <code>print()<\/code> statement then confirms this change by outputting \u201cNew baby, kids now: 1\u201d.<\/p>\n\n\n\n<p>This example demonstrates how class methods can be used to modify object data in a structured and encapsulated way.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner8-245.jpeg\" alt=\"\" class=\"wp-image-233462\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Creating a method object. Define the method object inc_num_kids() for PersonInfo. inc_num_kids increments the member data num_kids. Sample output for the given program with one call to inc_num_kids(): Kids: 0 New baby, kids now: 1 1 class PersonInfo: 2 3 def init(self): 4 self.num_kids = 0 5 6 # FIXME: Write inc_num_kids(self) 7 8 person1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-233461","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/233461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=233461"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/233461\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=233461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=233461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=233461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}