Date and time - extracting information, concept doubt


Hi everyone, I don't get the concept of extracting. The code below is from the example from date and time-extracting information:

from datetime import datetime
now =

current_year = now.year
current_month = now.month
current_day =

So, in the code above I understand that in the first 2 lines you import the whole of the library named date time. The next 3 lines I don't understand. I understand that there are 3 variables created named current_year, current_month, current_day. I also also understand that they are saving the current year, month and day values. What I don't understand is how they are getting these values. By typing (for example) now.year, is it the same thing as typing datetime.year since is saved within the variable now? If so, why do we type now.year instead of now.year() ?

Here's the link:

#2 is a method that returns an object with the current date and time.

>>> from datetime import datetime
datetime.datetime(2017, 5, 25, 13, 30, 30, 556169)

The instance attributes are ordered above as,

year, month, day, hour, minute, second, microseconds

When we cache this in a variable, the attributes are frozen in place.

now =

now is the name we give to this instance. We could have called it anything we want, but now is appropriate from a reading perspective.

Because year() is an attribute (now.year), not a method.


hi Roy, thanks for responding.In the code above (as I have understood) you have first imported the datetime library and then have you called the method, i'm not sure about the second line? what's a method? What are instance attributes? What does the phrase "cache this is in a variable" and how is it frozen in place. What is an instance and an attribute? I'm new to programming so it will be greatly appreciated if you could answer in simple english for new beginner programmers to understand.


Your questions relate to Python Classes and Modules which may not yet have been covered up to this point. I could not do any justice to the subject, under these conditions.

A module is a collection of classes, which classes can be instantiated. The instances are populated with attributes by the class __init__ method.


class My_Class(object):
    def __init__(self, attr1, attr2, attr3):
        self.attr1 = attr1
        self.attr2 = attr2
        self.attr3 = attr3
    def show_attributes(self):
        print "Attr1: %s, Attr2: %s, Attr3: %s" % (self.attr1, self.attr2, self.attr3)

my_class_instance = My_Class('a', 'b', 'c')    # instantiation of the class

print my_class_instance.attr1    # a
print my_class_instance.attr2    # b
print my_class_instance.attr3    # c

The above is a polling of each attribute, individually. Using a method, we can output the whole lot in one call...

# Attr1: a, Attr2: b, Attr3: c

As we see, the show_attributes method is accessible to our instance, which accesses only the defined attribute values of that instance.

For a better understanding, complete this unit with what you know and work at completing the track. Then you will be able to come back to this question with a better understanding (and a hope I haven't crushed this topic too badly).

a = 42

In the above, 42 is said to be cached in a. An assignment is a form of caching. It essentially means the same as stored in a.

When we cache now(), the values that are stored do not change.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.