Python -抽象

作者: 老生住长亭 | 来源:发表于2018-01-05 00:07 被阅读0次

Python 抽象

1、创建函数

关键字 def

2、文档化函数

3、参数

1、关键字参数和默认值

def hello_1(greeting, name):

    print('%s , %s!' % (greeting, name))

// name 默认值 Tome11 greeting默认值hello

def hello_2(name='Tome11', greeting='hello'):

    print('%s , %s!' % (greeting, name))

hello_1('hello', 'word')

//传值

hello_2('Tome', 'hello')

//使用默认值

hello_2()

//greeting默认值hello name为Luce

hello_2(name='Luce')

2、搜集参数 *参数名 表示可以传多个,打印出来是元组

def request(*params):

    print(params)

request("code")

request("code", "name")

结果是:

('code',)

('code', 'name')

def request1(param, *params):

    print(param)

    print(params)

request1("RequestParams", "name")

request1("RequestParams", "name", "code", "age")

结果是:

RequestParams

('name',)

RequestParams

('name', 'code', 'age')

// 传字典

def request3(**params):

    print(params)

request3(name="botter", age=30)

结果是:

{'name': 'botter', 'age': 30}

3、搜集参数的逆过程

def add(x, y):

    return x + y

parm = (1, 3)

print(add(*parm))

结果是:4

相关文章

  • 深入理解 Python 类和对象(2) - 抽象基类

    抽象基类,即 Python 中的 abc 模块,Abstract Base Class。 Python 抽象基类可...

  • Python -抽象

    Python 抽象 1、创建函数 关键字def 2、文档化函数 3、参数 1、关键字参数和默认值 def hell...

  • python-01基础

    python入门 The Zen of Python 数据模型 在Python中数据被抽象成对象,Python程序...

  • 2017.6.13-14

    学习python总结python常用的方法string的常用方法dictionary的常用方法 python抽象,...

  • 深入理解类和对象

    1.1 抽象基类(abc模块) python的抽象类的写法,继承抽象类的类必须要实现抽象方法,否则会报错 1.2 ...

  • 第一章 Python的创建型设计模式(1)

    1.1 抽象工厂模式 (Abstract Factory Pattern) 在《Python in pratice...

  • python基础(abc类)

    abc ABC是Abstract Base Class的缩写。Python本身不提供抽象类和接口机制,要想实现抽象...

  • 16.2、python初识面向对象(2)

    抽象类 什么是抽象类 与java一样,python也有抽象类的概念但是同样需要借助模块实现,抽象类是一个特殊的...

  • Python变量的存储

    Python变量的存储 在高级语言中,变量是对内存及其地址的抽象。 对于python而言,python的一切变量都...

  • python抽象基类abc

    python中并没有提供抽象类与抽象方法,但是提供了内置模块abc(abstract base class)来模拟...

网友评论

    本文标题:Python -抽象

    本文链接:https://www.haomeiwen.com/subject/efxjnxtx.html