Python初體驗(二)

來源:互聯網
上載者:User

在python函數的形參表中接受tuple,使用asteroid。

例如:

>>> def profile(name,*ages):
    print name
    print ages

   
>>> profile('Dush',12,23,34,45)
Dush
(12, 23, 34, 45)

接受dictionary

>>> def cart(**item):
    print item

   
>>> cart(apples = 4, peaches = 5, beef = 44)
{'peaches': 5, 'apples': 4, 'beef': 44}

接受tuple

>>> def example(a,b,c):
    return a+b*c

>>> tag = (1,2,3)
>>> example(tag)

Traceback (most recent call last):
  File "<pyshell#140>", line 1, in <module>
    example(tag)
TypeError: example() takes exactly 3 arguments (1 given)
>>> example(*tag)
7
接受dictionary

類似tuple,只不過需要用兩個*。

類和對象

>>> class exampleclass:
    eyes = "blue"
    ages = 22
    def thisMethod(self):
        return 'this method worked'

   
>>> exampleclass
<class __main__.exampleclass at 0x01F3A180>
>>> exampleobject = exampleclass()

>>> exampleobject.eyes
'blue'
>>> exampleobject.thisMethod()
'this method worked'

建構函式

>>> class new:
    def __init__(self):
        print "this is a constructor"

       
>>> newobj = new()
this is a constructor

 

>>> class className:
    def createName(self,name):
        self.name = name
    def displayName(self):
        return self.name
    def saying(self):
        print "hello %s" % self.name

       
>>> className
<class __main__.className at 0x0209E458>
>>> first = className()
>>> second = className()
>>> first.createName('Cabbage')
>>> second.createName('Tony')
>>> first.displayName()
'Cabbage'
>>> first.saying()
hello Cabbage
>>>

繼承

>>> class parentClass:
    var1 = "i am var1"
    var2 = "i am var2"

   
>>> class childClass(parentClass):
    pass

>>> parentobject = parentClass()
>>> parentobject.var1
'i am var1'
>>> childObject = childClass()
>>> childObject.var1
'i am var1'

python裡面的module如果在一個程式的使用過程中經過了修改的話,需要reload才能夠真正使得修改發揮作用

reload(blablabla)

查看module

以math為例

dir(math) 顯示math裡面的name

help(math.log) 顯示math裡面method的功能

math.__doc__ 簡短顯示這個module的功能

working with files

>>> fob = open('F:/document/python/a.txt','w')
>>> fob.write('Hey now brown cow')
>>> fob.close()
>>> fob = open('F:/document/python/a.txt','r')
>>> fob.read(3)
'Hey'
>>> fob.read()
' now brown cow'
>>> fob.close()

>>> fob = open('F:/document/python/a.txt','w')
>>> fob.write('this is a new line\nthis is line2\nthis is line3\this is line4\n')
>>> fob.close()
>>> fob = open('F:/document/python/a.txt','r')
>>> print fob.readlines()
['this is a new line\n', 'this is line2\n', 'this is line3\this is line4\n']

>>> fob = open('F:/document/python/a.txt','r')
>>> listme = fob.readlines()
>>> listme
['this is a new line\n', 'this is line2\n', 'this is line3\this is line4\n']
>>> fob.close()
>>> listme[2] = "mmmm what is wrong with you?"
>>> listme
['this is a new line\n', 'this is line2\n', 'mmmm what is wrong with you?']
>>> fob = open('F:/document/python/a.txt','w')
>>> fob.writelines(listme)
>>> fob.close()

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.