Call attribute method python -


i want display hour when call date.now.hour error

attributeerror: 'function' object has no attribute 'hour' 

this code:

#!/usr/bin/python  class date:     def now(self):         self.hour = "hour"         print "now"      def __call__(self, string):         print string      date = date() date('hello') # print hello date.now.hour # print hour 

my task is

make class can this:

date.now() - output: 'now'

date('hai') - output: 'hai'

date.now output: 'now'

date.now.hour output: 'hour'

you quite close - 1 of tasks done:

date('hai') - output: 'hai'

what still left is

date.now() - output: 'now'

date.now - output: 'now'

date.now.hour output: 'hour'

so date.now has quite lot of requirements can accomplished separate class:

class now(object):     def __str__(self): return ...     def __call__(self): return ...     hour = property(lambda self: 'hour') 

or this.

this class can use inside class date.


another option have now property of class date, work analogous. you'd need class now above, use like

class date(object):     def __call__ # before     @property     def now(self):         return now() 

Comments