程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> MSSQL >> SQL進修筆記四 聚合函數、排序辦法

SQL進修筆記四 聚合函數、排序辦法

編輯:MSSQL

SQL進修筆記四 聚合函數、排序辦法。本站提示廣大學習愛好者:(SQL進修筆記四 聚合函數、排序辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是SQL進修筆記四 聚合函數、排序辦法正文


很愛好Python這門說話。在看過語法後進修了Django 這個 Web 開辟框架。算是對 Python 有些熟習了。不外對外面許多器械照樣不曉得,由於用的少。明天進修了兩個魔術辦法:__new__ 和 __init__。

開攻:

假如對 Python 有所簡略懂得的話應當曉得它包括類這個概念的。語法以下:


class ClassName:
    <statement - 1>:
        .
        .  
        .
    <statement - N>

成績來了。像我們進修的 C# 或是 Java 這些說話中,聲明類時,都是有結構函數的。相似上面如許子:


public class ClassName{
    public ClassName(){

    }
}

固然拜訪潤飾符紛歧定非得 public ,這不是重點就不煩瑣了。那 Python 中的結構函數是如何的呢?我本身的懂得是它是沒有結構函數的。只不外在初始化時會挪用一些外部的可被轉變的辦法。好比:__new__ 和 __init__ 。從字面意思懂得 __new__ 應當會在 __init__ 之前履行,現實查了材料後確切是如斯的。官方文檔中關於 __init__ 辦法有如許一句話:


Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named __init__()

意思就是說在創立類時,假如想指定的它的初始狀況,那末可以經由過程界說一個指命名稱為 __init__ 的辦法去完成如許的功效。如許說來 __new__ 其實不是官方推舉的初始化類時要應用的辦法。然則 __new__ 倒是在 __init__ 之前履行的。官方文檔中對 __init__ 引見的第一句就是:當創立實例時挪用 __init__ 辦法(Called when the instance is created.),前面又引見說,假如想挪用基類的 __init__辦法必需顯式的挪用,只繼續基類在初始化子類時其實不會主動挪用基類的 __init__ 辦法。到此應當算是對 __init__ 辦法懂得了。

上面我們看一下 __new__ 辦法是怎樣回事兒。先看一下官方文檔:


Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of __new__() should be the new object instance (usually an instance of cls).

Typical implementations create a new instance of the class by invoking the superclass's __new__() method using super(currentclass, cls).__new__(cls[, ...]) with appropriate arguments and then modifying the newly-created instance as necessary before returning it.

If __new__() returns an instance of cls, then the new instance's __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to __new__().

If __new__() does not return an instance of cls, then the new instance's __init__() method will not be invoked.

__new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.

從這裡可以看出來,這個辦法才是發生類的實例的處所。當實例創立完造詣挪用 __init__ 辦法,初始化類的外部狀況值。文檔中還提到 __new__ 辦法實際上是一個靜態辦法,不消每次界說類的時刻都聲明這個辦法,由於在版本 2.4 以後 object 是一切對象的基類,而 __new__ 是界說在 object 對象外部的靜態辦法。

到這兒其實就差不多了,就按字面意思懂得便可以。 __new__ 用於創立對象,而 __init__ 是在創立完成以後初始化對象狀況。前兩天看到一個風趣的辦法,經由過程應用 __new__ 運用了單例形式在對象身上。

留意點:在類繼續中,當子類和父類都界說了本身的 __new__ 辦法時,那末會先挪用子類的 __new__ 辦法再挪用父類的。這一點卻是和 C# 中的繼續是一樣的。其實細心想一想 Python 中只不外是把初始化和創立對象這兩個概念離開了,而 C# 中即沒有這麼干,只給了結構函數,開辟者可以本身看著辦。從這一點兒上說我認為 Python 的做法我更愛好。

停止:

明天算是又進修到了新常識,本身挺高興的。再理論理論。。。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved