2017-10-31 4 views
0

lib.py輸入Pythonのlibには、パッチ後

#! /usr/bin/python 

def gethostbyname(hostname): 
    print "This is different gethostby name" 
    return "Hello" 

import socket 
# Patch the socket library 
socket.gethostbyname=gethostbyname 

def get(): 
    print socket.gethostbyname("www.google.com") 

test1.py

#! /usr/bin/python 
import socket 
print socket.gethostbyname("www.google.com") # <- this works fine 
from lib import get 
print get() 
print socket.gethostbyname("www.google.com") # <- method is changed 

108.177.98.105 # proper output from socket library 
This is different gethostby name 
Hello 
None 
This is different gethostby name # <- after import, the gethostbyname method is changed 
Hello 

test2.py

#! /usr/bin/python 
from lib import get 
print get() 
import socket 
print socket.gethostbyname("www.google.com") <- even import again, the socket gethostbyname is changed 

This is different gethostby name 
Hello 
None 
This is different gethostby name 
Hello 

を支配lib.pyファイル内のソケットにgethostbyname、私はインポートそのメソッドはtest * .pyからget()して実行します。ソケットライブラリがインポートされていれば、lib.pyインポートによってオーバーロードされますが、lib.pyを最初にインポートすると、あとでソケットをインポートすると元のソケットシステムライブラリが返されません。

なぜこのように動作していますか?

Python 2.7.10 (default, Feb 7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
+0

呼び出しますか? –

+0

@SandeepLadeはい、しました。私の場合、私は何かを働かせるためにMonkey Patchをやっています。 –

答えて

1

PYファイルあなたは、そのは、常にあなたのlib.pyファイルにgethostnameを使用して理由です下の行 socket.gethostbyname=gethostbyname を使用しています。それを削除する場合は、ライブラリでgethostnameメソッドを使用します。 lib.pyでgethostnameを使いたい場合はlib.gethostnameを使います。あなたの質問のための

PFB詳細な説明lib.py

#! /usr/bin/python 

def gethostbyname(hostname): 
    print "This is different gethostby name" 
    return "Hello" 

import socket 
# Patch the socket library 
socket.gethostbyname=gethostbyname 
#in the above program since you have given socket.gethostbyname=gethostbyname after that line when you call socket.gethostbyname it assumes that you are calling local gethostbyname 

def get(): 
    print socket.gethostbyname("www.google.com") 

#! /usr/bin/python 
import socket 
print socket.gethostbyname("www.google.com") 

# since you did not import lib before the above line this will work as it is defined in socket library 

from lib import get 
print get() 
print socket.gethostbyname("www.google.com") # <- method is changed 
# since you imported lib before the above line and in lib since you have `socket.gethostbyname=gethostbyname` in your lib.py file this will work as it is defined in lib.py 

#! /usr/bin/python 
from lib import get 
print get() 
import socket 
print socket.gethostbyname("www.google.com") 


# since you imported lib before the above line and in lib since you have `socket.gethostbyname=gethostbyname` in your lib.py file this will work as it is defined in lib.py 
01 test2.py でtest1.py ですべての

概要

  1. まずそれが(たとえばlib.pyあなたがのgethostname機能をカスタマイズしているしたくない場合は、PYで同じことを定義する組み込みのPythonライブラリ
  2. を変更することは良い考えではありません)socket.gethostbyname=gethostbynameを好きではない、すなわち、元のものを上書きせずに(ファイル)と輸入libがお好きな場所とあなたがPythonのソケットライブラリでのgethostname方法を変更しlib.gethostname
+0

あなたが正しいですが、ここではPythonのインポート動作を理解したいと思います。実際にインポートはPythonで何を意味しますか?その場合、インポートは実際に図書館のインポートをもたらし、その後は支配的です。もしそうなら、この動作を避けるには?より深くインポートされたライブラリが何か不快なことをしている場合、私たちが制御しているロジックをどうやって保護することができますか? –