码迷,mamicode.com
首页 > 编程语言 > 详细

Python基础篇(六)

时间:2014-08-03 17:51:05      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:java   使用   os   strong   ar   line   python   c   

  retun空值,后面的语句将不再被执行

  >>> def test():

  ...    print("just a test!")

  ...    return

  ...    print("will not be print")

  ...

  >>> test()

  just a test!

 

  和Java类似,在传递参数时,当参数是字符串,元组时,传递的其实是拷贝,修改实际参数不会影响到形式参数。当参数是对象时,修改实际参数将会影响到形式参数。

  >>> def changeName(name):

  ...    name = "Changed Name"

  ...    return name

  ...

  >>> name = "Jhon Brown"

  >>> changeName(name)

  ‘Changed Name‘

  >>> name

  ‘Jhon Brown‘

  实参为对象时的情况:

  >>> def changeList(list):

  ...    list[0] = "Change Element"

  ...    return list

  ...

  >>> list = ["one","two"]

  >>> changeList(list)

  [‘Change Element‘, ‘two‘]

  >>> list

  [‘Change Element‘, ‘two‘]

 

  可以为实参取别名,这样当参数为多个的时候不会因位置错误而导致错误的结果

  >>> def hello1(arg1,arg2):

  ...   print("%s %s" % (arg1,arg2))

  ...

  >>> hello1(arg1 = "hello",arg2 = "world")

  hello world

 

  使用*收集剩余的参数:

  >>> def printParams(title,*params):

  ...   print(title)

  ...   print(params)

  ...

  >>> printParams("Params",2,3,4)

  Params

  (2, 3, 4)

 

  定义和调用函数时都是用**,适用于元组或者字典。

  >>> def with_stars(**database):

  ...    print(database["name"]+ "\‘s age is" + database["age"])

  ...

  >>> database = {"name":"Grubby","age":"29"}

  >>> with_stars(**database)

  Grubby‘s age is29

  >>> def without_stars(database):

  ...    print(database["name"]+ "\‘s age is" + database["age"])

  ...

  >>> data = {"name":"Grubby","age":"29"}

  >>> without_stars(data)

  Grubby‘s age is29

 

  >>> def story(kwd):

  ...     print("Once there is a %(player)s called %(name)s," % kwd)

  ...

  >>> kwd = {"player":"king","name":"Chrile"}

  >>> story(kwd)

  Once there is a king called Chrile,

 

  vars函数会返回一个字典,包含变量与赋值的关系

  >>> x,y,z =1,2,3

  >>> scope =vars()

  >>> scope["x"]

  1

 

  递归调用的例子

  def search(sequence,number,low=0,upper = None):

     if upper is None :

        upper = len(sequence) -1

     if low == upper:

        if sequence[low] == number:

            return low

        else:       

            return -1         

     else:

         index = (int)((low + upper)/2)

         if sequence[index] > number:

            return search(sequence,number,low,index-1)  #这里的return是必须的,否则将没有返回值

         elif sequence[index] < number:

            return search(sequence,number,index+1,upper)

         else:

            return index

                 

  sequence = [1,3,9,11,56]

  search(sequence,3)

 

  创建自己的类

  >>> class Person:

  ...    def setName(self,name):

  ...        self.name = name

  ...    def getName(self):

  ...        return self.name

  ...    def greet(self):

  ...        print("name is %s" % self.name)

  ...

  >>> foo = Person()

  >>> bar = Person()

  >>> foo.setName("Luke")

  >>> foo.getName()

  ‘Luke‘

  >>> foo.greet()

  name is Luke

  >>> bar.setName("Sky")

  >>> bar.getName()

  Sky‘

  >>> bar.greet()

  name is Sky

  >>> bar.name

  ‘Sky‘

  >>> bar.name = "Jason"     #属性使用类加上.直接使用

  >>> bar.greet()

  name is Jason

 

  >>> class Bird:

  ...    __song = "spark"

  ...    def sing(self):

  ...       print(self.song)

  ...

  >>> bird = Bird()

  >>> bird.sing()

  Traceback (most recent call last):

    File "<stdin>", line 1, in <module>

    File "<stdin>", line 4, in sing

  AttributeError: ‘Bird‘ object has no attribute ‘song‘

  >>> class Bird:

  ...    song = "spark"

  ...    def sing(self):

  ...       print(self.song)

  ...

  >>> bird = Bird()

  >>> bird.sing()

  spark

Python基础篇(六),布布扣,bubuko.com

Python基础篇(六)

标签:java   使用   os   strong   ar   line   python   c   

原文地址:http://www.cnblogs.com/lnlvinso/p/3888548.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!