Date and time - extracting information, concept doubt


#1

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 = datetime.now()

current_year = now.year
current_month = now.month
current_day = now.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 datetime.now() is saved within the variable now? If so, why do we type now.year instead of now.year() ?

Here's the link:
https://www.codecademy.com/en/courses/python-beginner-en-zFPOx/0/3?curriculum_id=4f89dab3d788890003000096


#2

datetime.now() is a method that returns an object with the current date and time.

>>> from datetime import datetime
>>> datetime.now()
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 = datetime.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.


#3

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 datetime.now() 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.


#4

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.

Eg.

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...

my_class_instance.show_attributes()
# 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.


#5

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