Metaclasses - magic functions
Ben Finney
ben+python at benfinney.id.au
Tue Dec 20 18:39:33 EST 2016
"Mr. Wrobel" <mr at e-wrobel.pl> writes:
> Quick question, can anybody tell me when to use __init__ instead of
> __new__ in meta programming?
Use ‘__new__’ to do the work of *creating* one instance from nothing;
allocating the storage, determining the type, etc. — anything that will
be *the same* for every instance. ‘__new__’ is the constructor.
Use ‘__init__’ to do the work of *configuring* one instance, after it
already exists. Any attributes that are special to an instance should be
manipulated in the ‘__init__’ method. ‘__init__’ is the initialiser.
Because Python has a powerful type system built in, you generally have
little or nothing to do in ‘__new__’, and more work in ‘__init__’ for
each instance.
That's a general answer because you haven't said what your goal is. What
is it you want to do with types?
--
\ “Fur coats made for the ladies from their own skin.” —furrier, |
`\ Sweden |
_o__) |
Ben Finney
More information about the Python-list
mailing list