开发者

How to use property of object... Python!

In a module I defined a class with the following __init__ method:

class RMLoader(object):
    def __init__(self):
        self.now=datetime.datetime.now()

then I'm importing that module in console:

>>> from video.remmedia.loader import RMLoader
>>> loader=RMLoader()
>>> loader.now
datetime.datetime(2010, 11, 4, 17, 40, 36, 523000)

the question is why it not gives me standard datetime object?

but when i do print it act like standard datetime object:

>>> print loader.now
2010-11-04 17:40:36.52300开发者_开发百科0

But I steel cant use it in function where i need datetime object...

How can I use that attribute like standard datetime?


It is a standard datetime object all the time. The difference is that loader.now will invoke repr, and print loader.now will invoke str. The first one is designed to give an accurate representation of the object, ideally one that would evaluate to an identical object. The second one is designed to provide a readable representation of the object.

There is documentation on this here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜