1. 程式人生 > >Python類與標準庫

Python類與標準庫

Python類

>>> class MyClass:
...     """A simple example class"""
...     i = 12345
...     def f(self):
...         return 'hello world'
... 
>>> MyClass.__doc__
'A simple example class'
>>> MyClass.i
12345
>>> h = MyClass()
>>> h.f()
'hello world'

操作作業系統

>>> import os
>>> os.getcwd()
'/home/jiqing'
>>> os.system('mkdir today');
0
>>> os.chdir('/home/wwwroot/default')
>>> os.getcwd()
'/home/wwwroot/default'

常用數學計算

>>> import math
>>> math.log(1024,2);
10.0
>>> import random
>>> random.choice(['apple','pear','banana'])
'banana'
>>> random.random()
0.8585164844368266
>>> random.random()
0.1898083053044728