Vbl.time() is the same as dt.datetime.time(vbl)

Hi,

I’m having a bit of a hard time understanding classes, attributes and methods. I understand the theory but I’m stuck with the syntax, logic, etc.

Why is this:

tm = dt.datetime.time(vbl)

giving me the same output as this:

tm = vbl.time()

?

Thanks!

1 Like

Hey @joansch

Welcome to community :clap:t2:

Kindly post the link to the mission you are referring to. Also it would be better if you follow the Community Guidelines when asking a question.

Thanks for understanding

Best
K!

1 Like

Hi,

The template provided didn’t apply in this case because I had no expectations :slight_smile: I just don’t understand the syntax! How is it that the first line is module.class.method(variable) and the second one is variable.class() , they yield the same thing?

Thanks a lot
Thanks a lot

1 Like

Sorry to say this. But your code is hard to understand and it is difficult to help you in this case. I can help if you can provide more details…

Best
K!

1 Like

This:

appt_times = []

for appt in potus:
    vbl = appt[2]
    tm = dt.datetime.time(vbl)
    appt_times.append(tm)

yields the same output as this:

appt_times = []

for appt in potus:
    vbl = appt[2]
    tm = vbl.time()
    appt_times.append(tm)

Hope that’s clearer and thank you!

Hello @joansch,

dt.datetime.time is an instance method. According to the documentation, it will:

:speech_balloon:
Return time object with same hour, minute, second, microsecond and fold.

Since vbl is a datetime object it has attributes:

Expand to see the vbl attributes
>>>dir(vbl)

['__add__',
 '__class__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__ne__',
 '__new__',
 '__radd__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rsub__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__sub__',
 '__subclasshook__',
 'astimezone',
 'combine',
 'ctime',
 'date',
 'day',
 'dst',
 'fold',
 'fromordinal',
 'fromtimestamp',
 'hour',
 'isocalendar',
 'isoformat',
 'isoweekday',
 'max',
 'microsecond',
 'min',
 'minute',
 'month',
 'now',
 'replace',
 'resolution',
 'second',
 'strftime',
 'strptime',
 'time',
 'timestamp',
 'timetuple',
 'timetz',
 'today',
 'toordinal',
 'tzinfo',
 'tzname',
 'utcfromtimestamp',
 'utcnow',
 'utcoffset',
 'utctimetuple',
 'weekday',
 'year']

Check the datetime class attributes here

1 Like

Thank you, [info.victoromondi]

Stupid question - in the second line of code, how does the system know that vbl is a datetime object?

2 Likes

Everything in python is an object. To know an object belongs to which class use type() and thats how the system knows that vbl is a datetime object

>>>type(vbl)

datetime.datetime

2 Likes