Answer the question
In order to leave comments, you need to log in
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
Current time is 2013-12-29 22:06:37.479000
<bound method school.school_address of <__main__.school instance at 0x259c0c>>
Answer the question
In order to leave comments, you need to log in
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()
The case of self in the implementation of the method does not need to be passed?
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()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question