Python built-in functions


1 Introduction

  • isinstance
  • Hsattr
  • getattr
  • setattr
  • super

2. isinstance function

To determine whether an object is a known type.

grammar:isinstance(object, classinfo) -> bool

parameter:

  • object: instance of an object.
  • classinfo: may be directly or indirectly, class name, or basic types tuple composed thereof.

return value:

  • True: the type of object with the same classinfo.
  • False: not at the same time.

isinstance () and type () the difference:

  • type () does not think that is a subclass of parent class type, without regard to inheritance.
  • isinstance () will be considered sub-class is a parent class type, consider inheritance.

Example:

class A: pass
class B(A): pass
isinstance(A(), A)    # True
type(A()) == A        # True
isinstance(B(), A)    # True
type(B()) == A        # False

3. hasattr function

For determining whether the object contains the corresponding attribute.

grammar:hasattr(object, name: str) -> bool

parameter:

  • object: instance.
  • name: String, property name.

return value:

  • True: The object has properties.
  • False: no time.

Example:

class Coordinate:
    x = 10
point1 = Coordinate() 
hasattr(point1, 'x')    # True
hasattr(point1, 'y')    # False

4. getattr function

It returns an object property value.

grammar:getattr(object, name[, default])

parameter:

  • object: Object.
  • name: String, Object properties.
  • default: return the default value if the parameter is not available, when there is no corresponding attribute triggers AttributeError.

return value:

  • Returns the object property value.

Example:

class A:
    bar = 'bar的值'
a = A()
getattr(a, 'bar')                  # 'bar的值'
getattr(a, 'bar2', '没有该属性')    # '没有该属性'

5. setattr function

Set a property value, the property is not always present.

grammar:setattr(object, name, value) -> None

parameter:

  • object: Object.
  • name: String, Object properties.
  • value: property value.

return value:

  • no.

Example:

class A:
    bar = 'bar的值'
a = A()
getattr(a, 'bar')                  # 'bar的值'
getattr(a, 'bar2', '没有该属性')    # '没有该属性'
setattr(a, 'bar2', 'bar2的值')
getattr(a, 'bar2', '没有该属性')    # 'bar2的值'

6. super function

One way to call the parent class (super class).

grammar:super(type[, object-or-type])

parameter:

  • type: self class name of a subclass.
  • object-or-type: pointer example, generally self

return value:

  • The return of a call parent class method

Example python3:

class F:
    def __init__(self):
        print('F')

class A(F):
    def __init__(self):
        print('A')

class B(A):
    def __init__(self):
        super().__init__()

b = B() # 'A'

Example python2:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
class F(object):            # Python2.x 需要继承 object
    def __init__(self):
        print('F')

class A(F):
    def __init__(self):
        print('A')

class B(A):
    def __init__(self):
        super(B, self).__init__()

b = B() # 'A'
Published 31 original articles · won praise 11 · views 30000 +

Guess you like

Origin blog.csdn.net/MrRight17/article/details/104658494