1. 程式人生 > >Python - 筆記1

Python - 筆記1

cme wall uil generate game _file__ pen fatal .gz

1. 筆記
  • 常量為了和變量做區分,全部采用大寫字母,之間用下劃線連接。
  • 靜態方法:可以直接用類名來調用的方法,不需要創建對象,不會隱式的傳送self。
  • 根據面向對象的設計原則,應該將對象的職責封裝到類的代碼內部,盡量簡化調用一方的代碼調用。

1.1 方法的分類

實例方法
定義:第一個參數必須是實例對象,該參數名一般約定為“self”,通過它來傳遞實例的屬性和方法(也可以傳類的屬性和方法);
調用:只能由實例對象調用。

類方法

定義:使用裝飾器@classmethod。第一個參數必須是當前類對象,該參數名一般約定為“cls”,通過它來傳遞類的屬性和方法(不能傳實例的屬性和方法);
調用:實例對象和類對象都可以調用。

靜態方法

定義:使用裝飾器@staticmethod。參數隨意,沒有“self”和“cls”參數,但是方法體中不能使用類或實例的任何屬性和方法;
調用:實例對象和類對象都可以調用。

備註:MAC 安裝pygame報錯

liangkai@ttys000 $ pip install pygame
Collecting pygame
  Using cached https://files.pythonhosted.org/packages/b2/6b/c510f0853765eb2219ca5aa3d416d65bb0dea7cd9bb2984aea0a0e04c24d/pygame-1.9.4.tar.gz
Building wheels for collected packages: pygame
  Running setup.py bdist_wheel for pygame ... error
  Complete output from command /Library/Frameworks/Python.framework/Versions/3.5/bin/python3 -u -c "import setuptools, tokenize;__file__=‘/private/var/folders/1t/l_shx33n0911n3673wmdkl980000gr/T/pip-install-l6ap1uos/pygame/setup.py‘;f=getattr(tokenize, ‘open‘, open)(__file__);code=f.read().replace(‘\r\n‘, ‘\n‘);f.close();exec(compile(code, __file__, ‘exec‘))" bdist_wheel -d /private/var/folders/1t/l_shx33n0911n3673wmdkl980000gr/T/pip-wheel-7s7vp6w0 --python-tag cp35:
...............
building ‘pygame.sdlmain_osx‘ extension
    /usr/bin/clang -fno-strict-aliasing -Wsign-compare -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -arch i386 -arch x86_64 -g -Ddarwin -D_THREAD_SAFE -DENABLE_NEWBUF=1 -I/usr/X11R6/include -I/usr/local/include/SDL -I/Library/Frameworks/Python.framework/Versions/3.5/include/python3.5m -c src/sdlmain_osx.m -o build/temp.macosx-10.6-intel-3.5/src/sdlmain_osx.o
    In file included from src/sdlmain_osx.m:24:
    /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Carbon.framework/Headers/Carbon.h:34:10: fatal error: ‘CarbonSound/CarbonSound.h‘ file not found
    #include <CarbonSound/CarbonSound.h>
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~
    1 error generated.
    ---
    For help with compilation see:
        https://www.pygame.org/wiki/MacCompile
    To contribute to pygame development see:
        https://www.pygame.org/contribute.html
    ---
    error: command ‘/usr/bin/clang‘ failed with exit status 1

    ----------------------------------------
Command "/Library/Frameworks/Python.framework/Versions/3.5/bin/python3 -u -c "import setuptools, tokenize;__file__=‘/private/var/folders/1t/l_shx33n0911n3673wmdkl980000gr/T/pip-install-l6ap1uos/pygame/setup.py‘;f=getattr(tokenize, ‘open‘, open)(__file__);code=f.read().replace(‘\r\n‘, ‘\n‘);f.close();exec(compile(code, __file__, ‘exec‘))" install --record /private/var/folders/1t/l_shx33n0911n3673wmdkl980000gr/T/pip-record-f5kt4tgo/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/1t/l_shx33n0911n3673wmdkl980000gr/T/pip-install-l6ap1uos/pygame/

解決方法:

pip install pygame==1.9.2

Python - 筆記1