Being able to print before declaring. PYTHON

67 views Asked by At

It's quite straight forward, and I bet it's a noob question.

Why is it that the following can print the year BEFORE declaring it, as it is shown in the code

from datetime import datetime
now = datetime.now()
print now.year
current_year = now.year
current_month = now.month
current_day = now.day

This shows 2015, as it should, but how is it possible to pring out now.year, when I haven't even declared it at that point.

1

There are 1 answers

0
Aereaux On BEST ANSWER

datetime.now() returns a date object, which has a member named year. What you are doing here:

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

is assigning local variables, not assigning the members of now.