This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2008年11月26日 17:08 by cjw296, last changed 2022年04月11日 14:56 by admin. This issue is now closed.
| Pull Requests | |||
|---|---|---|---|
| URL | Status | Linked | Edit |
| PR 27866 | merged | rhettinger, 2021年08月21日 00:23 | |
| PR 27899 | merged | miss-islington, 2021年08月22日 19:27 | |
| PR 27900 | merged | lukasz.langa, 2021年08月22日 19:46 | |
| Messages (14) | |||
|---|---|---|---|
| msg76473 - (view) | Author: Chris Withers (cjw296) * (Python committer) | Date: 2008年11月26日 17:08 | |
>>> from datetime import datetime >>> class defaultdatetime(datetime): ... defaults=(2001,1,1) ... def __init__(self,*args): ... if not args: ... args = self.defaults ... datetime.__init__(self,*args) ... >>> defaultdatetime() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: function takes at least 3 arguments (0 given) |
|||
| msg76474 - (view) | Author: Chris Withers (cjw296) * (Python committer) | Date: 2008年11月26日 17:18 | |
Sorry, forgot to say that the above is at best counterintuitive and not documented anywhere... |
|||
| msg76475 - (view) | Author: Benjamin Peterson (benjamin.peterson) * (Python committer) | Date: 2008年11月26日 17:23 | |
This is because datetime does all its initialization in __new__ instead of __init__. |
|||
| msg76478 - (view) | Author: Chris Withers (cjw296) * (Python committer) | Date: 2008年11月26日 17:41 | |
But that isn't documented anywhere... |
|||
| msg76979 - (view) | Author: Georg Brandl (georg.brandl) * (Python committer) | Date: 2008年12月05日 08:06 | |
It's not documented for any immutable type. This should be fixed. |
|||
| msg162479 - (view) | Author: Mateusz Loskot (mloskot) * | Date: 2012年06月07日 17:16 | |
Is this report about documenting of the concept of immutable types in Python in general or regarding existing built-in types, like datetime.datetime? Generally, the concept of immutable type with relation to tp_new is mentioned (sneaked) here: 1) http://docs.python.org/release/3.2.2/c-api/typeobj.html "A good rule of thumb is that for immutable types, all initialization should take place in tp_new, while for mutable types, most initialization should be deferred to tp_init." 2) http://www.python.org/dev/peps/pep-0253/ Note that for immutable object types, the initialization cannot be done by the tp_init() slot: this would provide the Python user with a way to change the initialization. Therefore, immutable objects typically have an empty tp_init() implementation and do all their initialization in their tp_new() slot. IMHO, it deserves a dedicated section/chapter in the docs. |
|||
| msg162695 - (view) | Author: Chris Withers (cjw296) * (Python committer) | Date: 2012年06月12日 22:05 | |
It's the fact that for immutable types, initialization is done in __new__ instead of __init__ that isn't documented anywhere. This should be Python-level rather than C-level documentation. The example I gave in #msg76473 is confusing without docs anywhere that explain why this is. |
|||
| msg162697 - (view) | Author: R. David Murray (r.david.murray) * (Python committer) | Date: 2012年06月13日 00:32 | |
Actually, it is documented: http://docs.python.org/dev/reference/datamodel.html#basic-customization "__new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation." It could certainly be better documented, but where? The tutorial? |
|||
| msg162703 - (view) | Author: Mateusz Loskot (mloskot) * | Date: 2012年06月13日 12:12 | |
Chris Withers' note clarifies it to me, that this should be Python-level rather than C-level documentation. Then the note under __new__() in 3. Data model seems right. Simply, I expected to have some notes in C API too Side note: as mainly Python C API user, I expected to have it documented at C API level too. |
|||
| msg162704 - (view) | Author: Chris Withers (cjw296) * (Python committer) | Date: 2012年06月13日 12:17 | |
Probably also wouldn't go amiss to put some notes near the docs for common immutable types that people might subclass: datetime, maybe tuple? |
|||
| msg400099 - (view) | Author: Łukasz Langa (lukasz.langa) * (Python committer) | Date: 2021年08月22日 19:27 | |
New changeset eec340ea3af27887fcaac4029ebdee99f3713bff by Raymond Hettinger in branch 'main': bpo-4442: Document use of __new__ for subclasses of immutable types (GH-27866) https://github.com/python/cpython/commit/eec340ea3af27887fcaac4029ebdee99f3713bff |
|||
| msg400100 - (view) | Author: miss-islington (miss-islington) | Date: 2021年08月22日 19:49 | |
New changeset 0627918f0b69a15aa16e4ccbb5d8eaae4f6a2caf by Miss Islington (bot) in branch '3.10': bpo-4442: Document use of __new__ for subclasses of immutable types (GH-27866) https://github.com/python/cpython/commit/0627918f0b69a15aa16e4ccbb5d8eaae4f6a2caf |
|||
| msg400103 - (view) | Author: Łukasz Langa (lukasz.langa) * (Python committer) | Date: 2021年08月22日 20:14 | |
New changeset ac87b07a10e0ba2834e8de9cf0ea29a40fd882b1 by Łukasz Langa in branch '3.9': [3.9] bpo-4442: Document use of __new__ for subclasses of immutable types (GH-27866) (GH-27900) https://github.com/python/cpython/commit/ac87b07a10e0ba2834e8de9cf0ea29a40fd882b1 |
|||
| msg400104 - (view) | Author: Łukasz Langa (lukasz.langa) * (Python committer) | Date: 2021年08月22日 20:15 | |
Thanks for the patch, Raymond! ✨ 🍰 ✨ |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:56:41 | admin | set | github: 48692 |
| 2021年08月23日 06:34:56 | rhettinger | set | assignee: docs@python -> rhettinger |
| 2021年08月22日 20:15:13 | lukasz.langa | set | status: open -> closed versions: + Python 3.9, Python 3.10, Python 3.11, - Python 2.7, Python 3.2, Python 3.3 messages: + msg400104 resolution: fixed stage: patch review -> resolved |
| 2021年08月22日 20:14:35 | lukasz.langa | set | messages: + msg400103 |
| 2021年08月22日 19:49:28 | miss-islington | set | messages: + msg400100 |
| 2021年08月22日 19:46:16 | lukasz.langa | set | pull_requests: + pull_request26355 |
| 2021年08月22日 19:27:14 | lukasz.langa | set | nosy:
+ lukasz.langa messages: + msg400099 |
| 2021年08月22日 19:27:14 | miss-islington | set | nosy:
+ miss-islington pull_requests: + pull_request26354 |
| 2021年08月21日 00:23:11 | rhettinger | set | keywords:
+ patch nosy: + rhettinger pull_requests: + pull_request26321 stage: needs patch -> patch review |
| 2021年08月20日 16:57:08 | quantum | set | nosy:
+ quantum |
| 2012年06月13日 20:22:52 | cvrebert | set | nosy:
+ cvrebert |
| 2012年06月13日 12:17:55 | cjw296 | set | messages: + msg162704 |
| 2012年06月13日 12:12:19 | mloskot | set | messages: + msg162703 |
| 2012年06月13日 00:32:26 | r.david.murray | set | nosy:
+ r.david.murray messages: + msg162697 |
| 2012年06月12日 22:05:30 | cjw296 | set | messages: + msg162695 |
| 2012年06月07日 17:16:55 | mloskot | set | messages: + msg162479 |
| 2012年06月07日 17:11:48 | mloskot | set | nosy:
+ mloskot |
| 2011年11月18日 14:31:46 | eric.araujo | set | nosy:
+ eric.araujo |
| 2011年11月15日 19:35:02 | ezio.melotti | set | stage: needs patch versions: + Python 3.2, Python 3.3, - Python 3.1 |
| 2010年10月29日 10:07:21 | admin | set | assignee: georg.brandl -> docs@python |
| 2008年12月05日 08:06:41 | georg.brandl | set | messages:
+ msg76979 title: datetime not subclassable in the usual way -> document immutable type subclassing via __new__ |
| 2008年11月26日 17:41:31 | cjw296 | set | assignee: georg.brandl type: enhancement -> messages: + msg76478 components: + Documentation nosy: + georg.brandl |
| 2008年11月26日 17:23:58 | benjamin.peterson | set | priority: normal type: enhancement messages: + msg76475 nosy: + benjamin.peterson versions: + Python 3.1, Python 2.7, - Python 2.5 |
| 2008年11月26日 17:18:20 | cjw296 | set | messages: + msg76474 |
| 2008年11月26日 17:08:01 | cjw296 | create | |