-2

In C++ if(function()) returns false or true

but how is done in python? I ve tried:

if function() == True:

Help!

Why is self needed?

def func(self,word):
   if word == 'Hello':
      return True 
word = 'Hello'
if func(word):
   print(word)

why is asking to put self?? if func(self,word)?

tharmund
  • 3
  • 2
  • 1
    Do you not have an interpreter? If is a control-flow statement. It doesn't return anything. If the value of `function` in `if function():` is is truthy, then the block executes. `if function() == True` will only execute if the value returned by the function == True. – juanpa.arrivillaga Feb 10 '17 at 01:32
  • 1
    That "_in C++ if(function()) returns false or true_" is entirely false. – DYZ Feb 10 '17 at 01:34

1 Answers1

0

The following code works just fine:

def return_true():
    return True

if return_true():
   print("It's true!")

if return_true() == True:
   print("It's also true!")

Just make sure you're function is correctly returning True, maybe modify it so the return is just return True to check it's the if statement and not the function itself.

Darkstarone
  • 4,590
  • 8
  • 37
  • 74
  • def func(self, word): return True – tharmund Feb 10 '17 at 01:34
  • Yes, that function should work as intended (i.e. return true). Also you can use tildes (\`) to do code in comments: `def func(self,word): return True`. Remember to indent correctly as in Python, whitespace is important (the return should be on another line). – Darkstarone Feb 10 '17 at 01:36
  • I edited the post with an example, seems to work what you provided, but why it wants me to put the "self" word, and not just if func(arg):, but instead wants if func(self, arg)? – tharmund Feb 10 '17 at 01:40
  • It doesn't `self` is only required for `instance` methods in a `class`. This is why my example didn't use `self`. `def func(word): return True` is perfectly acceptable. If you are writing a `class`, then `self` is required. In `C++` terms `self` simply defines a function that is not static in a class (i.e. works on a single instance). – Darkstarone Feb 10 '17 at 01:45
  • Could take a look again at the post and just give me a quick explanation of why "self" is needed when calling a function in an IF statement ? – tharmund Feb 10 '17 at 01:46
  • I just did in my comment, for a better explanation of `self` see the answer [here](https://stackoverflow.com/questions/17134653/difference-between-class-and-instance-methods). It has nothing to do with the if statement. – Darkstarone Feb 10 '17 at 01:47
  • 1
    OK I get it, thanks a lot... Yes the function is in a class and requires the self like if func(self, arg): but without any class, it works like you stated. – tharmund Feb 10 '17 at 01:49
  • 1
    @Darkstarone technically `self` is just a naming convention used for the instance method of a class. You _can_ name it something other than `self` and the code works the same. – the_constant Feb 10 '17 at 01:55
  • @Vincenzzzochi I know, it says that in the linked question, I just didn't want to overcomplicate things! – Darkstarone Feb 10 '17 at 04:53