class Demo: y=1 # class (or static) variable def __init__(self,z): self.z = z @classmethod def class_method(cls, x): return x + cls.y @staticmethod def static_method(x): return x #cannot invoke cls.z def method(self, x): return x + self.z print Demo.class_method(2) # show 3 print Demo.static_method(2) # show 2 #print Demo.method(2) #Error, must declare first demo = Demo(2) print demo.method(2) # show 4
bloggerads
2017年8月4日 星期五
Python : class method / static method / method
This is a good example for showing the difference between class method, static method and method in Python.
2017年8月3日 星期四
Python : dictionary
Follows are the Dictionary example. Key and value can be string or number.
● Initial a dictionary
>>> d={'a':'Martin', 'b':'John'}
● Add new item
>>> d['c']='Mary'
● Add items
>>> d.update({'d':'Megan', 'e':'May'})
● Check item in a dictionary (return True/False)
>>> 'd' in d
True
>>> 'f' in d
False
● Enumerate the items in a dictionary
>>> for id, name in d.items():
... print id, name
● Delete an item
>>> del d['a']
● Initial a dictionary
>>> d={'a':'Martin', 'b':'John'}
● Add new item
>>> d['c']='Mary'
● Add items
>>> d.update({'d':'Megan', 'e':'May'})
● Check item in a dictionary (return True/False)
>>> 'd' in d
True
>>> 'f' in d
False
● Enumerate the items in a dictionary
>>> for id, name in d.items():
... print id, name
● Delete an item
>>> del d['a']
2017年7月31日 星期一
Python : list
In Python, We have list and and dictionary, similarly as the container list and map in C++, follows are the list example.
>>>list = [i for i in range(8) if i%2==0]
>>>list
[0, 2, 4, 6]
>>>list.append(2)
>>>list
[0, 2, 4, 6, 2]
● Insert data:3 to index: 2
>>>list.insert(2, 3)
>>>list
[0, 2, 3, 4, 6, 2]
>>>list.remove(3)
>>>list
[0, 2, 4, 6, 2]
2017年7月29日 星期六
Python : Some notes
(1) How to import a user's defined module 't1.py' in 't.py' ?
@t1.py
def watchout():
print "Did you call me?"
@t.py
#! python2
import t1
t1.watchout()
=========Output===========
> t.py
Did you call me?
==========================
(2) Popen, same as C++
@c.cpp, compile it to c.exe
#include <stdio.h>
int main()
{
printf("Hi Martin\nHi Megan\n");
return 0;
}
@p.py
#! python2
import os
output = os.popen('c.exe')
print output.read()
=========Output===========
> p.py
Hi Martin
Hi Megan
==========================
@t1.py
def watchout():
print "Did you call me?"
@t.py
#! python2
import t1
t1.watchout()
=========Output===========
> t.py
Did you call me?
==========================
(2) Popen, same as C++
@c.cpp, compile it to c.exe
#include <stdio.h>
int main()
{
printf("Hi Martin\nHi Megan\n");
return 0;
}
@p.py
#! python2
import os
output = os.popen('c.exe')
print output.read()
=========Output===========
> p.py
Hi Martin
Hi Megan
==========================
Python : 指定不同版本(Python 2 或 Python 3) 執行
若 PC上同時有 Python 2 及Python 3 才需要做以下動作。
● 進 python console
py -2
py -3
● 在 command prompt 執行.py檔
py -2 xxx.py
py -3 xxx.py
● 在檔案中說明該用哪種版本執行
#! python2
#! python3
● python2 使用中文時要在檔案頭宣告
#! python2
# coding: utf-8
● pip
py -2 -m pip install XXXX
py -3 -m pip install XXXX
● 進 python console
py -2
py -3
● 在 command prompt 執行.py檔
py -2 xxx.py
py -3 xxx.py
● 在檔案中說明該用哪種版本執行
#! python2
#! python3
● python2 使用中文時要在檔案頭宣告
#! python2
# coding: utf-8
● pip
py -2 -m pip install XXXX
py -3 -m pip install XXXX
Python : Introduction
最近空閒的時間都在玩Python, 一開始只是隨手看到公司的project中有很多附檔名為.py的tool, 好奇自己動手玩玩後, 就迷上這個語言了。決定把Python當做我的第二語言。
Python 為何風靡全球? 尤其在現今以C style (C/C++/JAVA/C#) 為主的分圍下仍然擁有眾多死忠的支持者,使用者人數排行過去多年來居高不下,想必他一定是有兩把刷子。 以下是Python 的特色 (個人心得):
1. Simple and easy to learn (語法簡單易學啊)
2. Object Oriented Programming (支援物件導向)
3. Versatile libraries (library非常多, 寫C的人最懂沒有library的痛!)
4. Massive communities support and massive population (廣大的社群支持, 就是粉絲團夠力)
5. As known Google/Youtube/N.A.S.A. use Python (一流的大公司都用Python)
6. Easy to package to an executable file (Without Python Interpreter) (打包成執行檔沒問題)
7. Python is everywhere (Preinstall on most Linux distribution) (大部分的Linux內建都有Python)
7. Python is everywhere (Preinstall on most Linux distribution) (大部分的Linux內建都有Python)
長期以來都有一個小問題困擾著我, 我是吃韌體這行飯的,高階語言對我來說只是用來分析log, 寫寫小tool方便工作用的。 過去我都是用C++, 如果需要GUI則用C#, 但是實在無法喜歡上這兩種語言。而 Python的哲學, 「優雅」、「明確」、「簡單」, 的確很和我胃口,人生應該要簡單一點,看了程式的一些介紹後就決定好好來認識他。
2017年5月7日 星期日
訂閱:
文章 (Atom)

