Day31-python Job

Source: Internet
Author: User

First, built-in functions isinstance and Issubclass.

1. Summarize the Python built-in function isinstance.

Isinstance is a built-in function in Python that can be used to determine whether an object is a known content.

L = List ([1, 2, 3])
Print (Type (l))
Print (Isinstance (l, list))
Print (Isinstance (l, tuple))

The above code will output the following results:

<class ' list ' >
True
False

Through the output can be seen, isinstance can determine the object ' l ' type is ' list ', when the type of ' l ' is the list of the time will be output true, and vice versa will output false.

2. Summarize the Python built-in function Issubclass.

The Python ctypes Library provides an interface to query for subclasses of its classes.

Class A:
Pass
Class B (A):
Pass
Print (Issubclass (B, A))
Print (Issubclass (A, B))

The above code will output the following results:

True
False

When you use Issubclass in your code to determine if Class B is a subclass of Class A, the output is true and the output is false when you determine whether a is a subclass of B.

Second, reflection

1. Reflections think of 4 built-in functions: GetAttr, Hasattr, SetAttr, delattr get members, check members, set members, delete members The following is a first look at an example:

  

class people:
def __init__ (self, name):
Self.name = Name
def walk (self):
Print (' Walk ')
p = people (' yh ') #实例化
Run the following code:
Print (GetAttr (p, ' name ')) #获取成员
Print (Hasattr (p, ' walk ')) #检查成员
SetAttr (P, ' age ',) #设置成员
SetAttr (p, ' id ', 1)
Print (P.age)
Delattr (p, ' id ') #删除成员
Output Result:

Yh
True
18

The added property ' id ' is deleted and then the output will be error:

Attributeerror: ' People ' object has no attribute ' id '

2. Reflection

(1). Import the module as a string.

_import_ (' Module name ')

Or

Import importlib

t = Importlib.import_moudle (' Module name ')

(2). To manipulate the members of an object (a module) in the form of a string

Three, built-in attr:__getattr__,__setattr__,__delattr__

__setattr__, __getattr__, and __delattr__ can intercept access to object properties.

_GETATTR_: When an attribute is not found in its usual position, it is automatically called (for example, a property that is neither the instance itself nor the property found in the class tree by self). Returns a property value or throws a Attributeerror.

_setattr_: A function that is called when a value is set to a property.

_setattr_: Called when a property is deleted.

Use the following code to demonstrate:

Class Foo:
def __init__ (self,name):
Self.name = Name
def __setattr__ (self, Key, value):
Self.__dict__[key] = value
def __delattr__ (self, item):
Self.__dict__.pop (item)
def __getattr__ (self, item):
Print ('%s '%item)
f = Foo (' yh ') #会触发 _setattr_ operation, adds ' name ': ' YH ' to _dict_
F.age = #会触发_setattr_的操作, add ' age ': ' 18 ' to _dict_
Del f.age #会触发_delattr_的操作, delete ' age ': ' 18 ' from _dict_
F.SS #会触发_getattr_的操作, will print ' SS '

Iv. Customizing your own data types:
1. Methods of Inheritance:
Class list (list):
def pop (self, index=none):
If Index >5:
Raise TypeError ("Don ' t Delete")
Else
Super (). Pop (index)

The list class in the code inherits the list class, and in the list class, it is defined that when the index value is greater than 5 o'clock, the hint is not allowed to delete, and the other case inherits the list method.

2. Way of Authorization
Import time
Class Open:
def __init__ (self, file, m= ' R ', encod= ' UTF8 '):

Self.file = File
Self.mode = m
self.encoding = Encod
self.x = open (file, mode=m, encoding=encod)
def write (Self,value):
t = time.strftime ('%y-%m-%d%x ')
Self.x.write ('%s%s '% (t, value))
def __getattr__ (self, item):
Return GetAttr (self.x, item)
o = Open (' A ', ' W ')
O.write (' 1111\n ') #触发 write operation to add the current time and ' 1111 ' to file a
r = Open (' A ', ' r+ ')
Print (r.read) #触发 _getattr_ operation





Day31-python Job

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.