Python Forum
Getting error when called through instance method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting error when called through instance method
#1
Hello Python Experts

Below code works without any issues when i called it though class method BUT when i called it through instance method , it is not working , may i please know why?

Working Code

class Pizza:
    def __init__(self, size):
        self.size = size
    def get_size(self):
        return "Size={}".format(self.size)

print(Pizza.get_size(Pizza(4555))
Output:
==> Size=4555
Error Code
class Pizza:
    def __init__(self, size):
        self.size = size
    def get_size(self, data):
        return "Size={}".format(self.size)

print(Pizza.get_size(4555))
Error:
[email protected]:~/code$ python3 phase2.py Traceback (most recent call last): File "phase2.py", line 7, in <module> print(Pizza.get_size(4555)) TypeError: get_size() missing 1 required positional argument: 'data'
Reply
#2
It's not class method vs. instance method. The get_size method is an instance method in both cases. And you don't call it from any instances. So it's an unbound instance method, which you have to explicitly provide an instance to. That's what you do in the first example (Pizza(4555) is the instance you are providing). In the second example you only provide an integer, and it is expecting an instance and an integer.

What you want to do is call it from an instance, so it is a bound method, and the instance parameter is provided implicitly.

class Pizza:
    def __init__(self, size):
        self.size = size
    def get_size(self):
        return "Size={}".format(self.size)

za = Pizza(4555)
print(za.get_size())
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Bunny Rabbit

Thanks for the clarification.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: sequence item 0: expected str instance, float found Error Query eddywinch82 1 205 Sep-04-2021, 09:16 PM
Last Post: eddywinch82
  What is this formatting called? Mark17 2 504 Dec-14-2020, 08:42 PM
Last Post: snippsat
  Instance of 'socket' has no 'error' member fedex03 1 976 May-13-2020, 03:23 PM
Last Post: deanhystad
  Error: Nested method ? JohnnyCoffee 5 1,157 May-03-2020, 02:43 PM
Last Post: JohnnyCoffee
  Class Instances called in the wrong order IanIous 4 985 Mar-06-2020, 02:16 PM
Last Post: IanIous
  How do you add the results together each time a function is called? Exsul 10 2,191 Aug-09-2019, 09:18 PM
Last Post: ichabod801
  Best method: Python script called from another app, package as complete executable ironfelix717 2 948 Jul-24-2019, 07:39 AM
Last Post: DeaD_EyE
  Called Functions Not Working WhatAmIDoing09 3 1,086 Jul-12-2019, 07:02 PM
Last Post: ichabod801
  Type error when reading in different data types on an __init__ method Dylanmull 3 1,196 May-09-2019, 02:05 PM
Last Post: buran
  Class object instance. Link instance attribute to class. Can it be done easier. Windspar 7 1,977 Dec-03-2018, 11:16 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020