A
A
Anton Manevant2013-12-29 21:11:45
Python
Anton Manevant, 2013-12-29 21:11:45

How does python return method values?

I recently started learning python and OOP in particular.
I don't understand the logic behind returning values ​​from class methods. Tell me, please.
Example.

from datetime import date

class school:
    address = "Hoggwart castle, 1 ave/34"
    school_number = 532
    teachers = 25
    scholars = 323
    __director = "Dumbledor"
    def __init__(self):
        self.init_time = datetime.now()
        print "Current time is %s" % self.init_time
    def school_address():
        a = "School andress is %s" % address
        print "print in school_address"
        return a

a = school()
a.school_address
print a.school_address

The time of the instance call gets to the console as expected, but the print of the school_address method does not get
Current time is 2013-12-29 22:06:37.479000
<bound method school.school_address of <__main__.school instance at 0x259c0c>>

Please explain what am I doing wrong?
Where can I read more about this mechanic?
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
Y
Yuri Shikanov, 2013-12-29
@Manevant

Working option:

from datetime import datetime

class school:
    address = "Hoggwart castle, 1 ave/34"
    school_number = 532
    teachers = 25
    scholars = 323
    __director = "Dumbledor"
    def __init__(self):
        self.init_time = datetime.now()
        print "Current time is %s" % self.init_time
    def school_address(self):
        a = "School andress is %s" % school.address
        print "print in school_address"
        return a

a = school()
print a.school_address()

S
Sergey, 2013-12-29
Protko @Fesor

So you didn't call the method...
print a.school_address()

M
Maxim Nikitenko, 2013-12-29
@sets88

method must be called with parentheses
print a.school_address()

O
one pavel, 2013-12-29
@onepavel

The case of self in the implementation of the method does not need to be passed?

A
Anton Manevant, 2013-12-29
@Manevant

Thanks, there were 2 errors, that's confusing :)

A
Ali Aliyev, 2014-12-30
@ali_aliev

It is immediately obvious that you are a ruby ​​if you call python methods like this :-) a.school_address will return a reference to the object of your method, and will not call it, in the same way you can do:

f = a.school_address
f()

and the method will be called.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question