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:
aankrose@PS1:~/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
  Multiple variable inputs when only one is called for ChrisDall 2 449 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  Couldn't install a go-game called dlgo Nomamesse 14 2,978 Jan-05-2023, 06:38 PM
Last Post: Nomamesse
  how can a function find the name by which it is called? Skaperen 18 3,322 Aug-24-2022, 04:52 PM
Last Post: Skaperen
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,009 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  TypeError: sequence item 0: expected str instance, float found Error Query eddywinch82 1 5,029 Sep-04-2021, 09:16 PM
Last Post: eddywinch82
  What is this formatting called? Mark17 2 1,732 Dec-14-2020, 08:42 PM
Last Post: snippsat
  Instance of 'socket' has no 'error' member fedex03 1 2,840 May-13-2020, 03:23 PM
Last Post: deanhystad
  Error: Nested method ? JohnnyCoffee 5 2,756 May-03-2020, 02:43 PM
Last Post: JohnnyCoffee
  Class Instances called in the wrong order IanIous 4 2,777 Mar-06-2020, 02:16 PM
Last Post: IanIous
  How do you add the results together each time a function is called? Exsul 10 5,004 Aug-09-2019, 09:18 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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