469,336 Members | 6,030 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

class unbound method and datetime.datetime.today()

Hi, I got confused when I learned the function datetime.today().

So far I learned, unless an instance is created, it is not possible to
call the class method. For example:

class Foo:
def foo(self):
pass

Foo.foo() # error: unbound method foo().

What makes me confused is that datetime class? in datetime module
provides today() function that returns the datetime object.
>>import datetime
datetime.datetime.today()
datetime.datetime(2007, 1, 9, 15, 34, 35, 23537)

It looks like that datetime class provides today() method that can be
callable even if it is unbound method. Do I correct?

If it is possible to make that kind of function (looks like static
member function in C++), how can I make that?

Thanks in advance.

Jan 9 '07 #1
1 2521
ci****@gmail.com schrieb:
Hi, I got confused when I learned the function datetime.today().

So far I learned, unless an instance is created, it is not possible to
call the class method. For example:

class Foo:
def foo(self):
pass

Foo.foo() # error: unbound method foo().

What makes me confused is that datetime class? in datetime module
provides today() function that returns the datetime object.
>>>import datetime
datetime.datetime.today()
datetime.datetime(2007, 1, 9, 15, 34, 35, 23537)

It looks like that datetime class provides today() method that can be
callable even if it is unbound method. Do I correct?

If it is possible to make that kind of function (looks like static
member function in C++), how can I make that?
It is called a classmethod (in contrast to an instancemethod, which is
the usual thing), and you can do it - depending on the version of python
you have - using the built-in funtion/decorator "classmethod". Like this:

class Foo(object):

@classmethod
def bar(cls):
pass
Note that a classmethod gets passed the class as first argument, not an
instance.

You can also create static methods, using "staticmethod". They won't get
passed anything.

Diez
Jan 9 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Fernando Rodriguez | last post: by
2 posts views Thread by Fernando Rodriguez | last post: by
4 posts views Thread by thehaas | last post: by
4 posts views Thread by Crypt Keeper | last post: by
18 posts views Thread by John M. Gabriele | last post: by
3 posts views Thread by Gregory Bond | last post: by
4 posts views Thread by venk | last post: by
2 posts views Thread by Ronald S. Cook | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.