简体   繁体   English

Python点表示法和日期时间对象

[英]Python dot notation and datetime object

Can someone explain to me the dot notation in this code? 有人能解释一下这段代码中的点符号吗?

now = datetime.now()

I know that now() refers to the method which belongs to the class datetime. 我知道now()指的是属于类datetime的方法。 Please take a look at the datetime.py module if needed . 如果需要,请查看datetime.py模块

what I'm confused about is the "datetime" part of now = datetime.now() . 令我困惑的是now = datetime.now()的“datetime”部分。 I read somewhere that it's supposed to be the instance of a class (the class is also called datetime if you look over the module) which the now() method belongs to, no? 我在某处读到它应该是一个类的实例(如果你查看模块,该类也称为datetime) now()方法属于哪个,不是吗? Since it's the instance shouldn't there be a line for the code which is 因为它的实例不应该有代码的行

datetime = datetime()

This is to specify that the datetime instance is part of the datetime class right? 这是为了指定datetime实例是datetime类的一部分吗?

I'm sorry if this question makes no sense at all, since it's only been a day since I've been working with Python. 如果这个问题毫无意义,我很抱歉,因为自从我使用Python以来只有一天。 Thank you for your time. 感谢您的时间。

You've run into what is called a class method . 你遇到了所谓的类方法

There's a few ways of figuring this out by looking at the code: 通过查看代码,有几种方法可以解决这个问题:

First off, is datetime.datetime.now() really an instance? 首先, datetime.datetime.now()真的是一个实例吗? if you were to type datetime.datetime in an interpreter, this is what you would get: 如果您要在解释器中键入datetime.datetime ,这将是您将获得的:

>>> datetime.datetime
<type 'datetime.datetime'>

Datetime is a class. 日期时间是一个类。 To instantiate it, you'd want to call it: 要实例化它,你想要调用它:

>>> datetime.datetime(2017, 7, 16, 1, 1, 47)
datetime.datetime(2017, 7, 16, 1, 1, 47)

The () brackets with arguments create an instance of the class. 带参数的()括号创建类的实例。 But when we call this now() method, there aren't any brackets! 但是当我们调用这个now()方法时,没有任何括号!

>>> datetime.datetime.now()
datetime.datetime(2017, 7, 16, 1, 3, 30, 501644)

That's because now() is a class method. 那是因为now()是一个类方法。 In other words, we don't require an instance of the class to call it. 换句话说,我们不需要类的实例来调用它。

If you're familiar with class instances and class definitions, you may have written this before: 如果您熟悉类实例和类定义,那么您可能以前写过:

class Hello(object):
    def say_message(self, message):
        print(message)

See that handy little self argument that comes first in that method? 看到那个方法中首先出现的方便的小self论证? self refers to the instance of the class that is passed down. self指向传递的类的实例。 That's called an instance method. 这称为实例方法。 A class method doesn't give us an instance, but rather the class definition. 类方法不提供实例,而是提供类定义。 So it doesn't require an instance. 所以它不需要实例。

Class methods are much more useful when you're doing advanced things with inheritance and shortcut initializations. 当您使用继承和快捷方式初始化进行高级操作时,类方法更有用。


Class methods are usually defined with a decorator: 类方法通常用装饰器定义:

@classmethod
def now(cls):
    ...

This is actually the same as writing this: 这实际上和写这个一样:

def now(cls):
    ...

now = classmethod(now)

Decorators were introduced as part of PEP 318 and brought into the language in Python 2.4. 装饰器是作为PEP 318的一部分引入的,并在Python 2.4中引入了该语言。 Before decorators were introduced, the second method above was always used. 在引入装饰器之前,总是使用上面的第二种方法。 That's because decorators are functions. 那是因为装饰器是功能。 The @ syntax is just to make this "decoration" more readable and clear. @语法只是为了使这个“装饰”更具可读性和清晰度。 I would read the PEP, since it has a whole bunch of good information on how and why decorators were introduced to the language. 我会阅读PEP,因为它有很多关于如何以及为什么装饰器被引入语言的好信息。 In the datetime.now case, you can see that the second method was used to transform the method into a class method (they do this with all the class methods, including other methods like fromtimestamp : datetime.now情况下,您可以看到第二个方法用于将方法转换为类方法(它们使用所有类方法执行此操作,包括其他方法,如fromtimestamp

def now(cls, tz=None):
    "Construct a datetime from time.time() and optional time zone info."
    t = _time.time()
    return cls.fromtimestamp(t, tz)
now = classmethod(now)

I'm assuming that you imported datetime using from datetime import datetime , in that case you really did import the datetime class. 我假设您导入datetime使用from datetime import datetime ,在这种情况下,你真的导入datetime类。

However, you don't have to first create an instance of that class if you want t use some of it's methods. 但是,如果您不想使用某些方法,则不必首先创建该类的实例。 Specifically the use of datetime.now() is quite common and is the right use. 具体来说, datetime.now()的使用很常见,是正确的用途。

In this case, the dot notation is directing python do use the method now of the class datetime and there's no contradiction in that. 在这种情况下,点符号是指导蟒蛇就使用的方法now类的datetime ,并没有矛盾这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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