简体   繁体   中英

Calling a method within a Class

I have the following class however when i try calling the method in an object, nothing happens

class parentClass:
  def test(self):
    if 3 > 2:
        print "This is true"
    else:
        print "This is false"
object1 = parentClass()
object1.test

Can someone please tel me what i am doing wrong?

You forget "()":

 object1.test()

Remember the methods need to be called with the brackets, for example in the propierties are not need it.

specifying a method with the parentheses just returns a reference to it. In order to call it, you need to have () (and optionally, any arguments) after it:

object1.test()

add parentheses '()' to the method name where you're calling it.

object1.test()

To call a function it requires '()'. Without these it's just a reference not a call.

object1.test ()

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM