mirror of
https://github.com/micropython/micropython.git
synced 2026-01-05 03:30:14 +01:00
py: Initial attempts to actually allow implementing __new__ in Python.
Caveat is that __new__ should recurse to base class __new__, and ultimately, object.__new__ is what handles instance allocation.
This commit is contained in:
20
tests/basics/object_new.py
Normal file
20
tests/basics/object_new.py
Normal file
@@ -0,0 +1,20 @@
|
||||
# object.__new__(cls) is the only way in Python to allocate empty
|
||||
# (non-initialized) instance of class.
|
||||
# See e.g. http://infohost.nmt.edu/tcc/help/pubs/python/web/new-new-method.html
|
||||
# TODO: Find reference in CPython docs
|
||||
|
||||
class Foo:
|
||||
|
||||
def __init__(self):
|
||||
print("in __init__")
|
||||
self.attr = "something"
|
||||
|
||||
|
||||
o = object.__new__(Foo)
|
||||
#print(o)
|
||||
print(hasattr(o, "attr"))
|
||||
print(isinstance(o, Foo))
|
||||
o.__init__()
|
||||
#print(dir(o))
|
||||
print(hasattr(o, "attr"))
|
||||
print(o.attr)
|
||||
Reference in New Issue
Block a user