Python dot notation and datetime object

Can someone explain to me the dot notation in this code?

now = datetime.now()

      

I know which now()

refers to a method belonging to the datetime class. Please take a look at datetime.py

if necessary
.

what I'm confused about is the "datetime" part now = datetime.now()

. I read somewhere that it should be an instance of the class (the class is also called datetime if you are looking at the module) that the method belongs to now()

, no? Since there should be no line in the instance for code that is

datetime = datetime()

      

This means the datetime instance is part of the datetime class, right?

I apologize if this question doesn't make any sense as it has only been a day since I was working with Python. Thank you for your time.

+3


source to share


2 answers


You are confronted with what is called a class method .

There are several ways to understand this by looking at the code:

First, is it datetime.datetime.now()

really an instance? if you type datetime.datetime

in the interpreter, this is what you get:

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

      

Datetime is a class. To create an instance, you want to call it:

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

      

The ()

argument parentheses create an instance of the class. But when we call this method now()

, there are no parentheses!

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

      

This is because it now()

is a class method. In other words, we don't need an instance of the class to call it.

If you are familiar with class instances and class definitions, you can write this earlier:



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

      

Take a look at this handy little argument self

, which comes first in this method? self

refers to the instance of the class that is passed down. This is called an instance method. A class method doesn't give us an instance, but rather a class definition. Therefore, it does not require an instance.

Class methods are much more useful when you are doing complex inheritance and shortcut initialization operations.


Class methods are usually defined using a decorator:

@classmethod
def now(cls):
    ...

      

This is actually the same as writing:

def now(cls):
    ...

now = classmethod(now)

      

Decorators were introduced as part of PEP 318 and translated into the language in Python 2.4. Prior to the introduction of decorators, the second method was always used. This is because decorators are functions. The syntax @

is to make this decoration more comprehensible and understandable. I would read the PEP as it has a whole bunch of good information on how and why decorators were introduced in this language. In the case, datetime.now

you can see that the second method was used to convert the method to a class method (they do this with all class methods, including other methods such as 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)

      

+4


source


I assume you imported datetime

with from datetime import datetime

, in which case you actually imported the class datetime

.

However, you don't need to instantiate this class first if you want to use some of its methods. In particular, the use is datetime.now()

quite common and is the correct use.



In this case, dot notation directs python to use a now

class method datetime

, and there is no contradiction in that.

0


source







All Articles