11.. _advanced_oo :
22
3- ===========================================
3+ ############################################
44Advanced Object Oriented Features of Python
5- ===========================================
5+ ############################################
66
77- Chris Barker
88
@@ -11,7 +11,7 @@ Advanced Object Oriented Features of Python
1111
1212
1313Multiple Inheritance
14- ====================
14+ #####################
1515
1616
1717Pulling methods from more than one class
@@ -323,6 +323,114 @@ and call super like
323323
324324
325325
326+ __new__
327+ --------
328+
329+ What *really * happens when a class instance is created?
330+
331+ Class Creation
332+ ----------------
333+
334+ What happens when a class instance is created?
335+
336+ ::
337+
338+ class Class(object):
339+ def __init__(self, arg1, arg2):
340+ self.arg1 = arg1
341+ self.arg2 = arg2
342+ .....
343+
344+ * A new instance is created
345+ * ``__init__ `` is called
346+ * The code in ``__init__ `` is run to initialize the instance
347+
348+
349+ Class Creation
350+ ----------------=
351+
352+ What if you need to do something before creation?
353+
354+ Enter: ``__new__ ``
355+
356+ ::
357+
358+ class Class(object):
359+ def __new__(cls, arg1, arg2):
360+ some_code_here
361+ return cls(....)
362+ .....
363+
364+ * ``__new__ `` is called: it returns a new instance
365+ * The code in ``__new__ `` is run to pre-initialize
366+ * ``__init__ `` is called
367+ * The code in ``__init__ `` is run to initialize the instance
368+
369+
370+ Class Creation
371+ ----------------
372+
373+ ``__new__ `` is a static method -- but it must be called with a class object as the first argument.
374+
375+ ::
376+
377+ class Class(superclass):
378+ def __new__(cls, arg1, arg2):
379+ some_code_here
380+ return superclass.__new__(cls)
381+ .....
382+
383+ ``cls `` is the class object.
384+
385+ The arguments (arg1, arg2) are what's passed in when calling the class.
386+
387+ It needs to return a class instance -- usually by directly calling the superclass ``__new__ ``
388+
389+ If nothing else, you can call ``object.__new__ ``
390+
391+
392+ When to use ``__new__ ``
393+ ------------------------
394+
395+ When would you need to use it:
396+
397+ * subclassing an immutable type:
398+
399+ - It's too late to change it once you get to ``__init__ ``
400+
401+ * When ``__init__ `` is not called:
402+
403+ - unpickling
404+
405+ - copying
406+
407+ You may need to put some code in ``__new__ `` to make sure things go right
408+
409+ More detail here:
410+
411+ http://www.python.org/download/releases/2.2/descrintro/#__new__
412+
413+
414+ LAB
415+ ----
416+
417+ **Demo: **
418+
419+ ``code/new_example.py ``
420+
421+ **Exercise: **
422+
423+ Write a subclass of int that will always be an even number: round the input to the closest even number:
424+
425+ ``code/__new__/even_int.py ``
426+
427+
428+ ``code/__new__/test_even_int.py ``
429+
430+
431+
432+
433+
326434Wrap Up
327435---------
328436
0 commit comments