473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3362
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Fernando Rodriguez | last post by:
Hi, I have a base class with a 'sanity-check' method. This method should iterate through all the methods and check if they are all 'thunks' (zero parameter functions, well actually, 1 parameter:...
2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
3
by: Robert | last post by:
Python doesn't know the class of a method when container not direct class attribute: >>> class X: .... def f():pass .... g=f .... l= .... >>> X.g <unbound method X.f>
4
by: thehaas | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm trying to use the datetime module. The following seems to work any day except the first day of the month: >>> import datetime >>> today =...
4
by: Crypt Keeper | last post by:
I am trying to write a program that asks user for several input items, then takes those items into a class that will manipulate the data and then return a line of output. I am able to input the...
18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
3
by: Gregory Bond | last post by:
I'm building a class hierarchy that needs to keep as a class variable a reference to a (non-member) function, so that different subclasses can use different generator functions. But it seems...
4
by: venk | last post by:
Hi, given below is my interaction with the interpreter.... In one case, i have created the class method using the "famous idiom"... and in the other, i have tried to create it outside the class...
2
by: Ronald S. Cook | last post by:
In trying to figure out how to make function MedicineClass.ApplyMedicinePriceChanges perform better, I ran Code Analysis and got the following: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.