-1

I am getting this error when I try to run function 'iswalking()'

I am trying to learn the OOPs concept and this is so far what I've created ...

class Human:
    def __init__(self,name,age,gender):
        self.name = name 
        self.age=age
        self.gender= gender
    def iswalking(self,TOF):
        self.TOF = TOF
        if TOF:
            print("walking")
        else:
            print("sitting")
    def introduce_self(self):
        print("my name is ",self.name)
        print("my age is ",self.age)
        print("my gender is ",self.gender)

h1=Human("Armaan",16,"male")
h1.iswalking=False
h1.introduce_self()
h1.iswalking()

I expect the output:

my name is  Armaan                                                                                                                     
my age is  16                                                                                                                          
my gender is  male 

sitting

what I get:

my name is  Armaan                                                                                                                     
my age is  16                                                                                                                          
my gender is  male                                                                                                                     
Traceback (most recent call last):                                                                                                     
  File "main.py", line 19, in <module>                                                                                                 
    h1.iswalking()                                                                                                                     
TypeError: 'bool' object is not callable                                                                                               


...Program finished with exit code 1                                                                                                   
Press ENTER to exit console.
NickD
  • 5,937
  • 1
  • 21
  • 38
Armaan Soni
  • 1
  • 1
  • 1
  • `h1.iswalking=False` here you are overwriting the function `iswalking` with a boolean value (False), so when you later try to call that function you get the error, as a boolean value is not callable. What were you trying to do with that line? – simon Oct 09 '19 at 16:38

2 Answers2

2

You've reassigned a function to a boolean, and then tried to call that boolean, thinking it is still a function.

Instead you want this:

class Human:
    def __init__(self,name,age,gender):
        self.name = name 
        self.age=age
        self.gender= gender
    def is_walking(self):
        if self.iswalking:
            print("walking")
        else:
            print("sitting")
    def introduce_self(self):
        print("my name is ",self.name)
        print("my age is ",self.age)
        print("my gender is ",self.gender)

h1=Human("Armaan",16,"male")
h1.iswalking=False
h1.introduce_self()
h1.iswalking()

This avoids reassigning the function.

ThePythonator
  • 154
  • 2
  • 13
1
h1=Human("Armaan",16,"male")
h1.iswalking=False # first 
h1.introduce_self()
h1.iswalking()     # second

In the first line, you've changed the attribute iswalking to False - which is a boolean.

In the second line, you're trying to call iswalking - which you had set to False = so you're trying to call False i.e. False() - which will raise the error you're seeing.

I believe you want to do this:

h1.iswalking(False)

Which should print:

sitting
rdas
  • 20,604
  • 6
  • 33
  • 46