Python built-in function learning summary (2)

Table of contents

1. id() function

2. object() function

 3. sorted() function

4. ascii() function

5. enumerate() function

6. input() function

7. oct() function

8. staticmethod () function

9. bin() function

10. setattr() function 

references:



1. id() function

        The id()  function is used to get the unique flag value of the specified object. Every object in python has a unique identifier, type and value. Among them, the unique identification is like people's ID number, which will not be repeated or changed.

>>>s=[]
>>>id(s)
1868782329792  #此为即为s的id值

2. object() function

The Object class         returns a new featureless object. The object class is the base class of all classes in Python. If you do not specify which class to inherit when defining a class, it will inherit the object class by default. object does not define __dict__, so you cannot try to set attributes on object class instance objects. It can be seen from the figure below that the object class cannot assign attribute values.

 3. sorted() function

        The sorted() function will reorder   the elements in the iterableand

        Syntax : sorted(iterable, key=None, reverse=False)

        iterable : specify an iterable object to be sorted

        key: optional parameter, specifies a function with only a single parameter, which is used to extract valid key values ​​for comparison from each element of the iterable parameter (for example, key = str.lower can be used to achieve case-insensitive sorting; or Sorting based on string length with key = len)

        reverse: When reverse=False, the elements are arranged from small to large. reverse=True means that the elements are arranged from large to small

Note : This function has the same effect as the built-in method sort(), but sort changes the original list, while reverse does not change the original list, but returns a new sorted list.

>>>a=[19,30,46,29,49,41]

>>>sorted(a)
[19, 29, 30, 41, 46, 49]
>>>a
[19, 30, 46, 29, 49, 41]#sorted()并未改变原始列表


>>>a.sort()
>>>a
[19, 29, 30, 41, 46, 49]#sort()更改了原始列表的顺序

4. ascii() function

The ascii() function is similar to the repr() function, returning a string         representing the object , but for the non-ASCII characters in the string, it returns the characters encoded by the repr() function using \x, \u or \U.

Syntax: ascii(object)

>>>ascii(50)
'50'

>>>ascii("xingxing")
"'xingxing'"

5. enumerate() function

        The enumerate()  function is used to return an enumeration object. That is, an object consisting of tuples is generated, and each tuple is composed of the index number of the iterable parameter and its corresponding element.

Syntax: enumerate(iterable, start=0)

>>>a="xingxing"  #索引号从默认0开始

>>>list(enumerate(a))
[(0, 'x'), (1, 'i'), (2, 'n'), (3, 'g'), (4, 'x'), (5, 'i'), (6, 'n'), (7, 'g')]

>>>list(enumerate(a,2))  #索引号从2开始
[(2, 'x'), (3, 'i'), (4, 'n'), (5, 'g'), (6, 'x'), (7, 'i'), (8, 'n'), (9, 'g')]

6. input() function

        The input() function is used to receive user input.

>>>a=input("你的名字")
你的名字

7. oct() function

        The oct() function converts an integer into an octal string, and the octal is represented by a prefix of 0o.

>>>oct(70)
'0o106'
>>>oct(10)
'0o12'

8. staticmethod () function

        A static method that returns a function. This method does not require parameters to be passed, declare a static method as follows

>>>class C(object):
        @staticmethod
        def f():
            print("walk")
>>>C.f()   #静态方法无需实例化
walk
#也可以实例化后调用
>>>cobj=C()
>>>cobj.f()
walk

9. bin() function

        The bin() function returns the binary representation of an integer int or long int.

>>>bin(100)
'0b1100100'

>>>bin(1)
'0b1'

10. setattr() function 

        The setattr() function corresponds to the function  getattr() , which is used to set the attribute value, and the attribute does not necessarily exist.

        Syntax: setattr(object, name, value)

        object: object

        name: object attribute, string

        value: attribute value

>>>class A(object):
        age=20

    
>>>a=A()
>>>getattr(a,"age")#获取a的属性值
20
>>>setattr(a,"age",23)#设置a的属性值
a.age
23

references:

Python object() function

Guess you like

Origin blog.csdn.net/weixin_51775350/article/details/128135235