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.

  • 相关阅读:
    swoole 安装方法 使用即时聊天
    git的介绍以及简单应用
    curl的应用
    linux下监听和同步代码配置
    mac skim 修改背景色
    php 编译安装的一个 configure 配置
    mac mysql error You must reset your password using ALTER USER statement before executing this statement.
    yii2 控制器里 action 大小写组合造成的路由问题
    warning : json_decode(): option JSON_BIGINT_AS_STRING not implemented in xxx
    redis 自启动脚本
  • 原文地址:https://www.cnblogs.com/Emily07/p/6406045.html
Copyright © 2011-2022 走看看