Python Forum

Full Version: newbie questions about objects properties...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,
Still confused between objects and properties..

When I run this:

from datetime import date
from datetime import time
from datetime import datetime

  
today = datetime.now()
f = today.time()
print f

print datetime.time(datetime.now())
 
Questions: When I'm running f = today.time() does this mean I'm calling the time method from the object "today" ? Or am I calling the time method from the datetime module and use today variable as "data input" ?

Why datetime.time(datetime.now()) is equivalent ? what is the "datetime.time()" ? a method straight from the datetime module ?

How should I code ? Do I need initialise objects by assigning a variable or can I call them straight like in the print datetime.time example ?


Also on the same lines..

import os 
print os.name;
Why above I can't do the below instead ? (this to understand properties...)

import os 
a = os.name
print a.name
you have already used name in a = os.name

import os 
a = os.name
print (a)
In a python script, when you see the expression
spam.eggs
it means that the object 'spam' has an attribute 'eggs'. The result of the expression is another object, the value of the attribute 'eggs' of the 'spam' object.

Often, this value is a callable object, which means that it can be used in a call expression such as in
spam.eggs(4)
It may happen, but this is not necessarily the case, that 'eggs' is a method of the class of the object 'spam'. In that case, the value of spam.eggs is a special callable object named an instancemethod.

Typically, when 'spam' is a module and 'eggs' is a function defined in this module, the expression spam.eggs() is an example of the same syntax, but eggs is not a method of the ModuleType. This illustrates the fact that this expression may have different semantics.