{"id":557,"date":"2024-12-29T09:17:24","date_gmt":"2024-12-29T09:17:24","guid":{"rendered":"https:\/\/datadandies.nl\/?p=557"},"modified":"2024-12-30T06:42:43","modified_gmt":"2024-12-30T06:42:43","slug":"using-an-instance-method-inside-another-instance-method-in-python","status":"publish","type":"post","link":"https:\/\/datadandies.nl\/index.php\/2024\/12\/29\/using-an-instance-method-inside-another-instance-method-in-python\/","title":{"rendered":"Using an instance method inside another instance method in Python"},"content":{"rendered":"\n<p>So you\u2019ve created a wonderful instance method for your class.<\/p>\n\n\n\n<p>However, you would like to use the instance attributes created in that first instance method, in a second instance method WITHOUT calling the first instance method.<\/p>\n\n\n\n<p>In that case you can use the following inside your second instance method: self.first_instance_method()<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers cbp-highlight-hover\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.3rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * 1.3rem);--cbp-line-highlight-color:rgba(234, 191, 191, 0.2);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">Python<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"# Using an instance method inside another instance method in Python\nclass VeryCoolClass:\n  \n    # Constructer method\n    def __init__(self):\n        pass\n\n    # First instance method\n    def first_instance_method(self):\n        # Instance attribute\n        self.instance_attribute = 'Look at this'\n\n    # Second instance method that calls the first instance method\n    def second_instance_method(self, add: str=' but not at this'):  # Default value of parameter &quot;add&quot; is ' but not at this'\n        self.first_instance_method()\n\n        # Printing the instance_attribute and adding the content of the &quot;add&quot; parameter\n        print(self.instance_attribute + add)\n    \n# Instantiation (creating a new instance)\ninstance_of_very_cool_class = VeryCoolClass()\n\n# Only calling the second instance; this means that the first_instance_method is called indirectly\ninstance_of_very_cool_class.second_instance_method(add=' but also at this!') # Output: Look at this but also at this!\" style=\"color:#D4D4D4;display:none\" aria-label=\"Kopieer\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\"># Using an instance method inside another instance method in Python<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">VeryCoolClass<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Constructer method<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">__init__<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">pass<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># First instance method<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">first_instance_method<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Instance attribute<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.instance_attribute = <\/span><span style=\"color: #CE9178\">&#39;Look at this&#39;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Second instance method that calls the first instance method<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">second_instance_method<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">add<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">str<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #CE9178\">&#39; but not at this&#39;<\/span><span style=\"color: #D4D4D4\">):  <\/span><span style=\"color: #6A9955\"># Default value of parameter &quot;add&quot; is &#39; but not at this&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.first_instance_method()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Printing the instance_attribute and adding the content of the &quot;add&quot; parameter<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.instance_attribute + add)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Instantiation (creating a new instance)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">instance_of_very_cool_class = VeryCoolClass()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Only calling the second instance; this means that the first_instance_method is called indirectly<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">instance_of_very_cool_class.second_instance_method(<\/span><span style=\"color: #9CDCFE\">add<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #CE9178\">&#39; but also at this!&#39;<\/span><span style=\"color: #D4D4D4\">) <\/span><span style=\"color: #6A9955\"># Output: Look at this but also at this!<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>So you\u2019ve created a wonderful instance method for your class. However, you would like to use the instance attributes created in that first instance method, in a second instance method WITHOUT calling the first instance method. In that case you can use the following inside your second instance method: self.first_instance_method()<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[54,37],"class_list":["post-557","post","type-post","status-publish","format-standard","hentry","category-blog","tag-oop","tag-python"],"_links":{"self":[{"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/posts\/557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/comments?post=557"}],"version-history":[{"count":13,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/posts\/557\/revisions"}],"predecessor-version":[{"id":572,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/posts\/557\/revisions\/572"}],"wp:attachment":[{"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/media?parent=557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/categories?post=557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/tags?post=557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}