• 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.

  • 相关阅读:
    [bbk3153] 第62集 Chapter 15Application Tuning(02)
    [bbk3152] 第61集 Chapter 15Application Tuning(01)
    [bbk1190]第2集 Chapter 01Oracle Architectural
    PL/SQL高级Creating Packages (01)
    Statspack00
    [bbk3201] 第64集 Chapter 16Using Materialized Views 00
    该如何选择国外VPS
    优化升级logging封装RotatingFileHandler
    smtp ssl模式邮件发送与附件添加
    smtp outlook邮件发送非授权码模式
  • 原文地址:https://www.cnblogs.com/Emily07/p/6406045.html
Copyright © 2020-2023  润新知