zoukankan      html  css  js  c++  java
  • What is a “method” in Python?

    http://stackoverflow.com/questions/3786881/what-is-a-method-in-python

    http://blog.chinaunix.net/uid-22521242-id-4017965.html

    在python中,一个对象的特征也称为属性(attribute)。它所具有的行为也称为方法(method)

    结论:对象=属性+方法。在python中,把具有相同属性和方法的对象归为一个类(class)

    属于某个类的函数就是方法 
    不属于任何类的函数就是函数

    python里的实例方法是带了self作为第一个参数的“函数”,类方法是带了cls作为第一个参数的“函数”

    In Python, a method is a function that is available for a given object because of the object's type.

    For example, if you create my_list = [1, 2, 3], the append method can be applied to my_list because it's a Python list: my_list.append(4). All lists have an append method simply because they are lists.

    As another example, if you create my_string = 'some lowercase text', the upper method can be applied to my_string simply because it's a Python string: my_string.upper().

    Lists don't have an upper method, and strings don't have an append method. Why? Because methods only exist for a particular object if they have been explicitly defined for that type of object, and Python's developers have (so far) decided that those particular methods are not needed for those particular objects.

    To call a method, the format is object_name.method_name(), and any arguments to the method are listed within the parentheses. The method implicitly acts on the object being named, and thus some methods don't have any stated arguments since the object itself is the only necessary argument. For example, my_string.upper() doesn't have any listed arguments because the only required argument is the object itself, my_string.

    One common point of confusion regards the following:

    import math
    math.sqrt(81)
    Is sqrt a method of the math object? No. This is how you call the sqrt function from the math module. The format being used is module_name.function_name(), instead of object_name.method_name(). In general, the only way to distinguish between the two formats (visually) is to look in the rest of the code and see if the part before the period (math, my_list, my_string) is defined as an object or a module.

  • 相关阅读:
    ThreadLocal
    volatile的理解
    二叉搜索树
    springboot中URL带有斜杠的转义字符%2F导致的400错误
    深入理解字符串
    Gradle安装与Idea配置
    基本数据类型和包装类
    Windows系统MongoDB安装
    Java程序是如何执行的?
    sshd服务器搭建管理和防止暴力破解
  • 原文地址:https://www.cnblogs.com/Emily07/p/6406045.html
Copyright © 2011-2022 走看看