Chris Jerdonek | 29 Jun 2012 18:05
Favicon

[issue15223] datetime instances lack __module__ attribute


New submission from Chris Jerdonek <chris.jerdonek <at> gmail.com>:

Instances of datetime.datetime don't seem to have the '__module__' attribute even though the datetime
class itself does.

This seems to contradict Section 3.2 of the Python documentation about the standard type hierarchy (in the
subsection called "Class instances"): http://docs.python.org/dev/reference/datamodel.html#the-standard-type-hierarchy

"A class instance has a namespace implemented as a dictionary which is the first place in which attribute
references are searched. When an attribute is not found there, and the instance’s class has an
attribute by that name, the search continues with the class attributes."

Instances of other classes defined in the standard library do have the attribute.

The session below illustrates the issue:

Python 3.3.0a4 (v3.3.0a4:7c51388a3aa7, May 30 2012, 16:58:42) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> datetime.__module__
'datetime'
>>> d = datetime(2000, 1, 1)
>>> d.__class__ 
<class 'datetime.datetime'>
>>> d.__module__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'datetime.datetime' object has no attribute '__module__'
(Continue reading)

Chris Jerdonek | 29 Jun 2012 19:55
Favicon

[issue15223] datetime instances lack __module__ attribute


Changes by Chris Jerdonek <chris.jerdonek <at> gmail.com>:

----------
versions: +Python 3.3

_______________________________________
Python tracker <report <at> bugs.python.org>
<http://bugs.python.org/issue15223>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/python-python-bugs-list%40m.gmane.org

Chris Jerdonek | 29 Jun 2012 19:54
Favicon

[issue15223] datetime instances lack __module__ attribute


Chris Jerdonek <chris.jerdonek <at> gmail.com> added the comment:

Also, FWIW, in PyPy the behavior is different.  Datetime instances do have the __module__ attribute:

Python 2.7.2 (341e1e3821fff77db3bb5cdb7a4851626298c44e, Jun 09 2012, 14:24:11)
[PyPy 1.9.0] on darwin
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``pypy is the nuclear fusion of
programming language implementation (pedronis)''
>>>> from datetime import datetime
>>>> datetime.__module__
'datetime'
>>>> d = datetime(2000, 1, 1)
>>>> d.__module__
'datetime'

----------

_______________________________________
Python tracker <report <at> bugs.python.org>
<http://bugs.python.org/issue15223>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/python-python-bugs-list%40m.gmane.org

Chris Jerdonek | 29 Jun 2012 19:54
Favicon

[issue15223] datetime instances lack __module__ attribute


Changes by Chris Jerdonek <chris.jerdonek <at> gmail.com>:

----------
versions: +Python 2.7 -Python 3.3

_______________________________________
Python tracker <report <at> bugs.python.org>
<http://bugs.python.org/issue15223>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/python-python-bugs-list%40m.gmane.org

R. David Murray | 30 Jun 2012 03:36
Favicon

[issue15223] datetime instances lack __module__ attribute


R. David Murray <rdmurray <at> bitdance.com> added the comment:

This is true for most (all?) CPython classes that are implemented in C.  For example io.StringIO instances
do not have a __module__ attribute either.

----------
nosy: +r.david.murray

_______________________________________
Python tracker <report <at> bugs.python.org>
<http://bugs.python.org/issue15223>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/python-python-bugs-list%40m.gmane.org

Chris Jerdonek | 30 Jun 2012 03:43
Favicon

[issue15223] datetime instances lack __module__ attribute


Chris Jerdonek <chris.jerdonek <at> gmail.com> added the comment:

Thanks.  I suspected that might be the case.  Then the question becomes whether failing to fall back to the
class attribute is the desired behavior, and if not, to update the documentation accordingly.

----------

_______________________________________
Python tracker <report <at> bugs.python.org>
<http://bugs.python.org/issue15223>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/python-python-bugs-list%40m.gmane.org

Favicon

[issue15223] datetime instances lack __module__ attribute


Changes by Arfrever Frehtes Taifersar Arahesis <Arfrever.FTA <at> GMail.Com>:

----------
nosy: +Arfrever

_______________________________________
Python tracker <report <at> bugs.python.org>
<http://bugs.python.org/issue15223>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/python-python-bugs-list%40m.gmane.org

Chris Jerdonek | 30 Jun 2012 06:09
Favicon

[issue15223] datetime instances lack __module__ attribute


Chris Jerdonek <chris.jerdonek <at> gmail.com> added the comment:

datetime.__dict__ is another attribute that behaves this way (i.e. doesn't exist for instances in
CPython but does for PyPy).

----------

_______________________________________
Python tracker <report <at> bugs.python.org>
<http://bugs.python.org/issue15223>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/python-python-bugs-list%40m.gmane.org

Amaury Forgeot d'Arc | 2 Jul 2012 14:33
Favicon

[issue15223] datetime instances lack __module__ attribute


Amaury Forgeot d'Arc <amauryfa <at> gmail.com> added the comment:

In PyPy, datetime.py is a pure Python module (similar to the one in 3.x, but without the _datetime
acceleration module).  So comparison with CPython is not relevant here.

In CPython, __module__ is not an attribute of the type, but a property: it is defined in the 'type' object. 
The situation is similar to the following script (2.x syntax); the "foo" attribute can be found on the
class, but not on instances of the class.

class Type(type):
    foo = 42
class Datetime:
    __metaclass__ = Type
print Datetime.foo
print Datetime().foo

This is a good thing sometimes: for example 'str' has a __dict__ (containing methods) but strings don't
have a __dict__ -- storage is optimized and only has an array of chars.  In this case you wouldn't want the
class __dict__ be returned instead.

----------
nosy: +amaury.forgeotdarc

_______________________________________
Python tracker <report <at> bugs.python.org>
<http://bugs.python.org/issue15223>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
(Continue reading)

Eric Snow | 13 Nov 2012 05:29
Favicon

[issue15223] datetime instances lack __module__ attribute


Changes by Eric Snow <ericsnowcurrently <at> gmail.com>:

----------
nosy: +eric.snow

_______________________________________
Python tracker <report <at> bugs.python.org>
<http://bugs.python.org/issue15223>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/python-python-bugs-list%40m.gmane.org


Gmane