你知道吗自由基你知道吗
#! /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")
测试1.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
测试2.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
在中修补套接字gethostbyname之后自由基文件中,我从test*.py导入其方法get(),然后运行。如果已导入套接字库,则自由基但是,如果导入自由基首先,以后导入socket不会带回原来的socket系统库,这让我很困惑。你知道吗
为什么会这样?你知道吗
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.
py文件您正在使用下面的行
socket.gethostbyname=gethostbyname
这就是为什么它总是在你的语言中使用gethostname
自由基文件。如果删除,它将使用socket
库中的gethostname
方法。如果要在中使用gethostname自由基使用lib.gethostname
。你知道吗请详细解释您的问题 英寸自由基
在test1.py中
在test2.py中
摘要
socket.gethostbyname=gethostbyname
),并在需要时导入lib并调用lib.gethostname
相关问题 更多 >
编程相关推荐