Python中的__new__()詳解
Lately I started looking into Django code and wish to write about internals of Django. I started with Django models and will be writing about it soon.
For understanding how Django models work, I had to understand what metaclasses are and how metaclasses work.
Metaclasses use method "__new__" and so I looked at what "__new__" does.
As __new__ is a static method, we will see a lttle bit about static methods and then __new__ in detail.
1. Understanding static methods.
2. Understanding method "__new__" of any class. We will see how to override method __new__ in a class.
Also, I will be trying all the code we write here on Ipython and I suggest you to try everything on Ipython as well.
Static methods
A little bit about instance methods first. Let's write a class.
...: def met(self, a, b):
...: print a, b
...:In this case, met() is an instance method. So, it is expected that we pass an instance of A as the first argument to met.
Let's create an object and call met() on the created object and pass two arguments to met().
In [5]: obj.met(1,2)
1 2 #output
For understanding how Django models work, I had to understand what metaclasses are and how metaclasses work.
Metaclasses use method "__new__" and so I looked at what "__new__" does.
As __new__ is a static method, we will see a lttle bit about static methods and then __new__ in detail.
1. Understanding static methods.
2. Understanding method "__new__" of any class. We will see how to override method __new__ in a class.
Also, I will be trying all the code we write here on Ipython and I suggest you to try everything on Ipython as well.
Static methods
A little bit about instance methods first. Let's write a class.
CODE:
In [1]: class A(object):...: def met(self, a, b):
...: print a, b
...:In this case, met() is an instance method. So, it is expected that we pass an instance of A as the first argument to met.
Let's create an object and call met() on the created object and pass two arguments to met().
CODE:
In [4]: bj = A()In [5]: obj.met(1,2)
1 2 #output
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-732625/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 詳解Python中的__init__和__new__Python
- Python 中__new__方法詳解及使用Python
- python中__new__的使用注意Python
- python中__init__ 和__new__的對比Python
- python中的__init__ 、__new__、__call__小結Python
- Python中的Super詳解Python
- 詳解Python中的程式Python
- Python中的列表詳解Python
- Python中__new__和__init__的區別與聯絡Python
- 簡述Python類中的 __init__、__new__、__call__ 方法Python
- Python中的__new__、__init__、__call__三個特殊方法Python
- Python中的函式詳解Python函式
- python中dict詳解Python
- Python __new__ 和 __init__ 的區別Python
- Python 中的魔術方法詳解Python
- Python中的魔術方法詳解Python
- 詳解Python中的下劃線Python
- python中list切片詳解Python
- python2中的__new__與__init__,新式類和經典類Python
- 詳解Python中的str.format方法PythonORM
- python中變數的命名及詳解Python變數
- Python類中__del__()、__call__()、__repr__()、__new__()、__hash__()方法Python
- Python3中urlopen()詳解Python
- python 中model.py詳解Python
- Python中Numpy函式詳解Python函式
- Python中協程(coroutine)詳解Python
- 一問搞懂python的__init__和__new__方法Python
- 詳解Python中yield生成器的用法Python
- 詳細解讀Python中的__init__()方法Python
- Python中none和null的區別詳解!PythonNoneNull
- python基礎(8)python中is和==的區別詳解Python
- python中setting.py詳解Python
- python 中求和函式 sum詳解Python函式
- Python面試之理解__new__和__init__的區別Python面試
- Python 中的設計模式詳解之:策略模式Python設計模式
- Python3中使用PyMongo的方法詳解PythonGo
- Python3中*和**運算子的用法詳解!Python
- 詳解Python中sys模組的功能與應用Python