Python Forum
TypeError: Not supported between instances of 'function' and 'int'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: Not supported between instances of 'function' and 'int'
#9
The trick in Griboulis's code is the @property decorator. Decorators wrap the function immediately following it in another function to add some behavior to it. @property in particular creates an attribute with the name of the following function. The benefit of using @property is that it enables setter and getter methods all using the name of the attribute, "spam" in this case. Setters and getters can be used to run additional code when the attribute changes.

This is useful for attributes that need to be managed so that the object is correctly updated for the new value. For instance, if your class has an attributes that must correspond to get accurate results - say for car braking system - then you can have a managed attribute that will automatically change the other attributes when it is changed. Thereby, you can ensure everything is correctly updated whenever this critical value changes.

class Bar:
    def __init__(self):
        self._spam = 100
        
    @property
    def spam(self):
        return self._spam

    @spam.setter
    def spam(self, value):
        print(self._spam)
        self._spam = value
        print(self._spam)

    @spam.getter
    def spam(self):
        return self._spam

x = Bar()
x.spam
x.spam = 200
x.spam
Reply


Messages In This Thread
RE: TypeError: Not supported between instances of 'function' and 'int' - by stullis - Dec-04-2019, 03:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using curve_fit to optimize function (TypeError) Laplace12 4 2,716 Aug-30-2021, 11:15 AM
Last Post: Larz60+
  [Solved] TypeError when calling function Laplace12 2 3,035 Jun-16-2021, 02:46 PM
Last Post: Laplace12
  Sort Function: <' not supported between instances of 'float' and 'tuple' quest 2 8,326 Apr-30-2021, 07:37 PM
Last Post: quest
Exclamation TypeError: '>=' not supported between instances of 'int' and 'str' helpme1 11 9,151 Mar-11-2021, 11:13 AM
Last Post: helpme1
  Type error: '>' not supported between instances of 'NoneType' and 'int' spalisetty06 1 10,617 Apr-29-2020, 06:41 AM
Last Post: buran
  TypeError: '<' not supported between instances of 'str' and 'int' Svensation 5 9,217 Jan-20-2020, 08:12 PM
Last Post: buran
  TypeError: '>=' not supported between instances of 'str' and 'int' AsadZ 8 11,114 Aug-20-2019, 11:45 AM
Last Post: ThomasL
  '>' not supported between instances of 'str' and 'int' graham23s 2 4,101 May-11-2019, 07:09 PM
Last Post: micseydel
  Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int sr12 8 13,427 Apr-11-2019, 08:19 PM
Last Post: sr12
  '<' not supported between instances of 'str' and 'int' jayaherkar 1 8,042 Apr-09-2019, 03:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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