class unbound method and datetime.datetime.today()

Discussion in 'Python' started by cinsky@gmail.com, Jan 9, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. 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
     
    Diez B. Roggisch, Jan 9, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    771
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Kevin Walzer

    Error: unbound method in Tkinter class

    Kevin Walzer, Dec 9, 2006, in forum: Python
    Replies:
    2
    Views:
    366
    Bjoern Schliessmann
    Dec 9, 2006
  3. Replies:
    2
    Views:
    583
  4. Joshua Kordani
    Replies:
    0
    Views:
    318
    Joshua Kordani
    Jan 7, 2010
  5. Steve Holden
    Replies:
    0
    Views:
    694
    Steve Holden
    Jan 7, 2010
Loading...

Share This Page