Summary of knowledge about Python object oriented built in functions

  • 2021-11-13 08:15:08
  • OfStack

Python built-in functions

1. classmethod, staticmethod, property.

The above three built-in functions have been used in detail in the methods and properties of the article (Python Advanced-Object-Oriented Members), and can be returned for browsing. The specific address is https://www.ofstack.com/article/215871. htm

2. callable, whether it can be executed with parentheses after it.

Function


def func():
    pass
 
print( callable(func) ) # True

Class


class Foo(object):
    pass
 
print( callable(Foo) ) # True

Class has a __call__ The object of the method


class Foo(object):
	pass
 
obj = Foo()
print( callable(obj) ) # False

class Foo(object):
 
    def __call__(self, *args, **kwargs):
        pass
    
obj = Foo()
print( callable(obj) ) # True

Therefore, when you see the following situations in the future, you should first think that handler can be three kinds: function, class and object with call method. What exactly is it needs to be analyzed according to the calling relationship of the code.


def do_something(handler):
    handler()

3. super, look up members according to mro inheritance relationship.


class Top(object):
    def message(self, num):
        print("Top.message", num)
        
class Base(Top):
    pass
 
class Foo(Base):
 
    def message(self, num):
        print("Foo.message", num)
        super().message(num + 100)
 
 
obj = Foo()
obj.message(1)
 
>>> Foo.message 1
>>> Top.message 101

class Base(object):
 
    def message(self, num):
        print("Base.message", num)
        super().message(1000)
 
 
class Bar(object):
 
    def message(self, num):
        print("Bar.message", num)
 
 
class Foo(Base, Bar):
    pass
 
 
obj = Foo()
obj.message(1)
 
>>> Base.message 1
>>> Bar.message 1000

Application scenario

Suppose there is a class that has already implemented some functions, but we want to extend some functions based on it and rewrite it once? It is troublesome, so you can use super at this time.


info = dict() # {}
info['name'] = " Huaqing water "
info["age"] = 18
 
value = info.get("age")
 
print(value)

class MyDict(dict):
 
    def get(self, k):
        print(" Custom functionality ")
        return super().get(k)
 
 
info = MyDict()
info['name'] = " Huaqing water " # __setitem__
info["age"] = 18       # __setitem__
print(info)
 
value = info.get("age")
 
print(value)

4. type, which gets the type of 1 object.


v1 = " Huaqing water "
result = type(v1)
print(result) # <class 'str'>

class Foo(object):
    pass
 
print( callable(Foo) ) # True
0

class Foo(object):
    pass
 
print( callable(Foo) ) # True
1

5. isinstance, which determines whether an object is an instance of a class or its subclass.


class Foo(object):
    pass
 
print( callable(Foo) ) # True
2

class Animal(object):
    def run(self):
        pass
 
class Dog(Animal):
    pass
 
class Cat(Animal):
    pass
 
data_list = [
    "alex",
    Dog(),
    Cat(),
	"root"
]
 
for item in data_list:
    if type(item) == Cat:
        item.run()
    elif type(item) == Dog:
        item.run()
    else:
        pass
    
for item in data_list:
    if isinstance(item, Animal):
        item.run()
    else:
        pass

6. issubclass, which determines whether a class is a descendant of a class.


class Foo(object):
    pass
 
print( callable(Foo) ) # True
4

At this point, some built-in functions about object-oriented have been summarized. If there are any inadequacies, please correct me!


Related articles: