美文网首页python
05 函数和方法

05 函数和方法

作者: 菩提树下参悟 | 来源:发表于2023-04-13 13:36 被阅读0次

函数和方法的区别
函数是独立存在的功能语句组,使用函数直接调用即可
方法是依附于某个数据类型下的函数
方法的调用格式:标识符 . 标识符()

定义函数可以作为方法

在student.py的文件里

def write():
    print('I can write.\n')

def study():
    print('I can study.\n')

def student():
    print('I am a strudent.\n')
    write()
    study()
方法的调用

在test.py文件里

import  student
student.write()
student.student()
输出:
I can write.

I am s strudent.

I can write.

I can study.

Python 中函数的应用非常广泛,前面章节中我们已经接触过多个函数,比如 input() 、print()、range()、len() 函数等等,这些都是 Python 的内置函数,可以直接使用。
各种类型本身就是函数,或者说类。

相关文章

网友评论

    本文标题:05 函数和方法

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