简体   繁体   中英

imported python lib dominates

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

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

After patch the socket gethostbyname in lib.py file, I import its method get() from test*.py and run. If the socket library has been imported, it will be overrode by the lib.py import, however, if you import the lib.py first, import the socket later won't bring back the original socket system library which makes me confused.

Why it is behaving like this?

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 file you are using the below line socket.gethostbyname=gethostbyname That's why its always using the gethostname in your lib.py file. If you remove that it will use gethostname method in socket library. If you want to use gethostname in lib.py use lib.gethostname .

PFB detailed explanation for your question In 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")

in test1.py

#! /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

in test2.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

Summary

  1. First of all it's not a good idea to change builtin python libraries
  2. If you don't want to have customized gethostname function you define the same in a py(say lib.py) file (without overriding the original ones ie not like socket.gethostbyname=gethostbyname )and import lib wherever you want and call lib.gethostname

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM