什么是“方法"?在 Python 中? [英] What is a "method" in Python?

查看:19
本文介绍了什么是“方法"?在 Python 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能用非常简单的术语向我解释 Python 中的方法"是什么?

Can anyone, please, explain to me in very simple terms what a "method" is in Python?

在许多面向初学者的 Python 教程中,这个词的使用方式好像初学者已经知道 Python 上下文中的方法是什么.虽然我当然熟悉这个词的一般含义,但我不知道这个词在 Python 中的含义.所以,请向我解释Pythonian"方法的全部内容.

The thing is in many Python tutorials for beginners this word is used in such way as if the beginner already knew what a method is in the context of Python. While I am of course familiar with the general meaning of this word, I have no clue what this term means in Python. So, please, explain to me what the "Pythonian" method is all about.

一些非常简单的示例代码将非常受欢迎,因为一张图片值一千字.

Some very simple example code would be very much appreciated as a picture is worth thousand words.

推荐答案

这是一个类的成员函数:

It's a function which is a member of a class:

class C:
    def my_method(self):
        print("I am a C")

c = C()
c.my_method()  # Prints("I am a C")

就这么简单!

(还有一些替代方法,允许您控制类和函数之间的关系.但我从您的问题中猜测您不是在问那个,而只是基础知识.)

(There are also some alternative kinds of method, allowing you to control the relationship between the class and the function. But I'm guessing from your question that you're not asking about that, but rather just the basics.)

这篇关于什么是“方法"?在 Python 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆