python interface to design and del __all__

Recently encountered some minor problems in the implementation python interface, resolved summarized below.

Purpose: When designing the interface, a file specific method of exposure only.

 

E.g:

t.py

import os
import sys

def a():
    pass

def b():
    pass

def c():
    pass

Suppose we want to develop plug-ins called Shona, if we, follow the steps in the design of the plug-in __init__.py file:

from t import *

As the import, when calling the method can be seen:

Shona.t.a()
Shona.t.b()
Shona.t.c()
Shona.t.os
Shona.t.sys

 

 

If we want to expose the method and a method b , describes two methods as follows :( practical applications, which can easily see which work with which can also be a combination of both, in fact, I used the situation is more complex, both are used here not explained in detail Ψ (¯ (Ester) ¯) Ψ)

 

1. Add the following code t.py:

__all__ = ['a', 'b']

# Write to other locations can also note in the file is a list of the head or tail oh

 

To do so, specify which method is exposed.

Of course, we recommend the following import methods:

from t import a
from t import b

 

However, these methods are sometimes not fully meet the demand (here you will know in practice), you can use the following method that is more violence. 

 

2. Add the following code __init__.py

This is the observation tensorflow source found, the original tensorflow also have problems like this:

the c
 of the os
 of the sys

 

That is where the mistake introduced accidentally deleted, not exposed to the user.

 

The above is a small sum friends - I wish you all a happy development.

 

 

Guess you like

Origin www.cnblogs.com/shona/p/11969758.html