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
#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


Messages In This Thread
RE: Getting error when called through instance method - by ichabod801 - Mar-02-2019, 06:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiple variable inputs when only one is called for ChrisDall 2 566 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  Couldn't install a go-game called dlgo Nomamesse 14 3,576 Jan-05-2023, 06:38 PM
Last Post: Nomamesse
  how can a function find the name by which it is called? Skaperen 18 3,816 Aug-24-2022, 04:52 PM
Last Post: Skaperen
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,405 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  TypeError: sequence item 0: expected str instance, float found Error Query eddywinch82 1 5,340 Sep-04-2021, 09:16 PM
Last Post: eddywinch82
  What is this formatting called? Mark17 2 1,861 Dec-14-2020, 08:42 PM
Last Post: snippsat
  Instance of 'socket' has no 'error' member fedex03 1 2,982 May-13-2020, 03:23 PM
Last Post: deanhystad
  Error: Nested method ? JohnnyCoffee 5 2,937 May-03-2020, 02:43 PM
Last Post: JohnnyCoffee
  Class Instances called in the wrong order IanIous 4 2,988 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,426 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