hausaufgabe--python 40 -- some BIF

00-- issubclass(class,classinfo)

to check if the class are the child of the Class in classinfo( can be a list )

01-- isinstance (object, classinfo)

to check if the object in the classinfo (it can be a list with all the related class object name), return Truth of False

02-- hasattr(object, name)

e.g. 

Note: name need to be check with ' '

003 -- getattr(oject, name [default]) 

return the value of the accessed attribute. And you also can define the message for return if the attribute can't be found. 

 004 -- setattr(object, name, value)

to set the value of the attribute for the object. 

a--if the attribute have a value, then update to the newly value

b-- if the attribute doesn't exist in the object, then new a new attribute for the object and set the value. 

 005 -- delattr(object, name)

delect the related attribute in the object 

006 -- property(fget=None,fset=None,fdel=None,doc=None)

pass the functions of get,set,del to property function, and it will call related function with related parameter.

e.g.

class C:
    def __init__(self, size=10):
        self.size = size

    def getXSize(self):
        return self.size

    def setXSize(self, value):
        self.size = value

    def delXSize(self):
        del self.size

    x = property(getXSize,setXSize,delXSize)
 

 007 -- function modifier

Normally, if we would like to count the running time of a function, we will write the code as below:

import time
 
def timeslong(func):
    start = time.clock()
    print("It's time starting ! ")
    func()
    print("It's time ending ! ")
    end = time.clock()
    return "It's used : %s ." % (end - start)

Then we need to write the detail codes for func(). 

But with function modifier:

import time
 
def timeslong(func):
    def call():
        start = time.clock()
        print("It's time starting ! ")
        func()
        print("It's time ending ! ")
        end = time.clock()
        return "It's used : %s ." % (end - start)
    return call

@timeslong
def f():
    y = 0
    for i in range(10):
        y = y + i + 1
        print(y)
    return y

print(f())

Or we can use it in a class

class timeslong(object):
def __init__(self,func):
    self.f = func
def __call__(self):
    start = time.clock()
    print("It's time starting ! ")
    self.f()
    print("It's time ending ! ")
    end = time.clock()
    return "It's used : %s ." % (end - start)

@timeslong
def f():
    y = 0
    for i in range(10):
        y = y + i + 1
        print(y)
    return y

print(f())

And we also can use @staticmethod, @classmethod, @property to update the properties or functions for related class and also can be used with class itself directly (no need to type with a instance)

e.g 

set classmethod

class Hello:
    def __init__(self):
        pass
    @classmethod
    def print_hello(cls):
        print("Hello")

the class itself can call the functions without new a instance. 

set staticmethod

class Hello:
    def __init__(self):
        pass
    @staticmethod
    def print_hello():
        print("Hello")

 

PS: please note the @calssmethod and @staticmethod difference.  

008-- @ in python

@ is a function. 

原文地址:https://www.cnblogs.com/Shareishappy/p/7495624.html