Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; 'cpython': 0.05; 'arguments': 0.07; 'builtin': 0.07; 'class,': 0.07; 'level,': 0.07; 'override': 0.07; 'strings.': 0.07; 'python': 0.09; '__init__': 0.09; 'derived': 0.09; 'methods,': 0.09; 'non- string': 0.09; 'port),': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'server,': 0.15; 'evaluates': 0.16; 'inconvenient': 0.16; 'it".': 0.16; 'passed.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:effect': 0.16; 'subject:import': 0.16; 'tcp': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'specify': 0.17; 'creates': 0.18; 'jan': 0.18; 'code,': 0.18; 'variable': 0.20; 'supposed': 0.21; 'example': 0.23; 'class.': 0.23; 'errors': 0.23; 'idea': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; 'mind.': 0.27; 'header:X-Complaints-To:1': 0.28; 'actual': 0.28; 'probably': 0.29; 'class': 0.29; "i'm": 0.29; 'connection': 0.30; "skip:' 10": 0.30; 'code': 0.31; 'gets': 0.32; 'server.': 0.32; "skip:' 20": 0.32; 'could': 0.32; 'skip:s 30': 0.33; 'quotes': 0.33; 'handle': 0.33; 'to:addr:python-list': 0.33; 'likely': 0.33; 'version': 0.34; 'done': 0.34; 'server': 0.35; 'requiring': 0.35; 'pm,': 0.35; 'add': 0.36; 'received:org': 0.36; 'explain': 0.36; 'but': 0.36; 'method': 0.36; 'subject:with': 0.36; 'should': 0.36; 'correctly': 0.37; 'signature': 0.37; 'level': 0.37; 'two': 0.37; 'passed': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'notice': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Strange effect with import Date: Thu, 20 Dec 2012 19:54:06 -0500 References: <50d38d78$0$29967$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356051272 news.xs4all.nl 6888 [2001:888:2000:d::a6]:54090 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35248 On 12/20/2012 5:52 PM, Jens Thoms Toerring wrote: > You are rather likely right and I probably should have written: > "I don't see any way to pass that variable to the object that > is supposed to use it". Perhaps you have an idea how it could > be done correctly when I explain the complete picture: I'm > writing a TCP server, based on SocketServer: > > server = SocketServer.TCPServer((192.168.1.10, 12345), ReqHandler) > > where ReqHandler is the name of a class derived from > SocketServer.BaseRequestHandler You misunderstood the doc. You pass the class, not the name of the class. From 21.19.4.1. socketserver.TCPServer Example server = socketserver.TCPServer((HOST, PORT), MyTCPHandler) MyTCPHandler is the actual class. What gets 'passed' at the C level in CPython is a reference that class that TCPServer can use to call it, but conceptually, at the Python level, think of it as the class. In the code, you enter the name without quotes and that expression evaluates to the (reference to the) class that gets passed. If the signature required the name, the example would have had 'MyTCPHandler', with the quotes, to pass the name as a string. Very few builtin functions require names as strings. open('filename'), somebytes.encode(encoding='encoding-name', errors = 'error-handler-name') are two that come to mind. Notice that these are situations where requiring a non-string object would be inconvenient at best. > class ReqHandler(SocketServer.BaseRequestHandler): > ... > > A new instance of this class is gernerated for each connection > request to the server. In the call that creates the server I can > only specify the name of the class but no arguments to be passed Code those arguments directly into the handle method of your version of MyTCPhandler. Or if you need to override multiple methods and use the same values in multiple methods, override __init__ and add self.x = x-value statements. -- Terry Jan Reedy