顯示具有 super 標籤的文章。 顯示所有文章
顯示具有 super 標籤的文章。 顯示所有文章

2014年2月27日 星期四

[python]super super(type,[obj or type])


2. Built-in Functions — Python v2.7.6 documentation
http://docs.python.org/2/library/functions.html#super



super(type[, object-or-type])
Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by getattr() except that the type itself is skipped.
The __mro__ attribute of the type lists the method resolution search order used by both getattr() and super(). The attribute is dynamic and can change whenever the inheritance hierarchy is updated.
If the second argument is omitted, the super object returned is unbound. If the second argument is an object, isinstance(obj, type)must be true. If the second argument is a type, issubclass(type2,type) must be true (this is useful for classmethods).
Note

super() only works for new-style classes.
There are two typical use cases for super. In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable. This use closely parallels the use of super in other programming languages.
The second use case is to support cooperative multiple inheritance in a dynamic execution environment. This use case is unique to Python and is not found in statically compiled languages or languages that only support single inheritance. This makes it possible to implement “diamond diagrams” where multiple base classes implement the same method. Good design dictates that this method have the same calling signature in every case (because the order of calls is determined at runtime, because that order adapts to changes in the class hierarchy, and because that order can include sibling classes that are unknown prior to runtime).
For both use cases, a typical superclass call looks like this:
class C(B):
    def method(self, arg):
        super(C, self).method(arg)
Note that super() is implemented as part of the binding process for explicit dotted attribute lookups such as super().__getitem__(name). It does so by implementing its own __getattribute__() method for searching classes in a predictable order that supports cooperative multiple inheritance. Accordingly, super() is undefined for implicit lookups using statements or operators such as super()[name].
Also note that super() is not limited to use inside methods. The two argument form specifies the arguments exactly and makes the appropriate references.
For practical suggestions on how to design cooperative classes using super(), see guide to using super().
New in version 2.2.


ref.
3. Data model — Python v2.7.6 documentation
http://docs.python.org/2/reference/datamodel.html#newstyle
Glossary — Python v2.7.6 documentation
2. Built-in Functions — Python v2.7.6 documentation



2014年1月22日 星期三

[python][django] django forms super


在使用 django forms 時,有看到幾個django中super的用法,查了一些相關資料。


It’s important to remember to call the superclass method – that’s thatsuper(Blog, self).save(*args, **kwargs) business – to ensure that the object still gets saved into the database. If you forget to call the superclass method, the default behavior won’t happen and the database won’t get touched.



順帶查了 *args, **kwargs

It’s also important that you pass through the arguments that can be passed to the model method – that’s what the *args, **kwargs bit does. Django will, from time to time, extend the capabilities of built-in model methods, adding new arguments. If you use *args, **kwargs in your method definitions, you are guaranteed that your code will automatically support those arguments when they are added.


Models | Django documentation | Django
https://docs.djangoproject.com/en/1.5/topics/db/models/
Programmer blog: Python super() method and class inheritance
http://garmoncheg.blogspot.tw/2012/06/python-super-method-and-class.html
Understanding Python’s ‘super’ | These are Dark Times, Lad
http://blog.timvalenta.com/2009/02/understanding-pythons-super/