Path: csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'case.': 0.05; 'api': 0.09; "'.'": 0.09; 'subject:Getting': 0.09; 'subject:module': 0.09; 'subject:string': 0.09; 'django': 0.10; 'all?': 0.16; 'subject:class': 0.16; 'subject:instance': 0.16; 'wary': 0.16; 'string': 0.17; 'wrote:': 0.17; 'instance': 0.17; 'developer': 0.19; 'module': 0.19; 'code.': 0.20; 'changes': 0.20; 'trying': 0.21; 'import': 0.21; 'received:192.168.1.100': 0.22; 'received :mail-bk0-f46.google.com': 0.22; 'class.': 0.23; 'this:': 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'developers': 0.26; 'received:209.85.214.46': 0.27; "d'aprano": 0.29; 'steven': 0.29; 'class': 0.29; "i'm": 0.29; "skip:' 20": 0.32; 'could': 0.32; 'certain': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'equal': 0.33; 'received:google.com': 0.34; 'generic': 0.35; 'nov': 0.35; 'path': 0.35; 'doing': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'method': 0.36; 'possible': 0.37; 'does': 0.37; 'being': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'advice': 0.39; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'received:192': 0.39; 'called': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'header:Received:5': 0.40; 'end': 0.40; 'close': 0.63; 'more': 0.63; 'offer': 0.65; 'hoping': 0.72; 'construct': 0.84; 'existence.': 0.84; 'upstream': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=bmu3Tpisg1qpARWTF3g2YUYJiVKooFhvWyWvl8e11fg=; b=DWGj7fSJOFGhB4wi9WJ6qwr063miWzSz6BwSIpsckIBOgO/Cu/JhCijXR7EUNRFoSV 1A5vjCOiNthI+/xkBz5oV4Ye41qO8p93nugFnJYTfe/UtBSc4XlpVBoKOVHVTuszQgQZ nXRZTFHlod9jYEyVrJuS/UfYSjx5eYIc3DkLrp3Okbb9z9BNwJXeh2S1QNpmt8DVdpIL 0NbnKmlD/ZlRHRJ7iztmX4rjkm1jpzOJqAPUeRPd2ekBukYlAyQknFvNAtki5sxgm18Z OjJif9QgcVJN6werBvPvz1y7/KUPxyPAVRiu1b/qXQiQxwKu4brgIswqFFIP/qUwSU4a p6FQ== Date: Tue, 13 Nov 2012 07:54:32 +0000 From: Some Developer User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:16.0) Gecko/20121026 Thunderbird/16.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Getting module path string from a class instance References: <50a1f46e$0$21742$c3e8da3$76491128@news.astraweb.com> In-Reply-To: <50a1f46e$0$21742$c3e8da3$76491128@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352793277 news.xs4all.nl 6890 [2001:888:2000:d::a6]:53855 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33226 On 13/11/2012 07:19, Steven D'Aprano wrote: > On Tue, 13 Nov 2012 06:38:31 +0000, Some Developer wrote: > >> I'm trying to find a way to get a string of the module path of a class. >> >> So for instance say I have class Foo and it is in a module called >> my.module. I want to be able to get a string that is equal to this: >> "my.module.Foo". I'm aware of the __repr__ method but it does not do >> what I want it to do in this case. >> >> Can anyone offer any advice at all? > py> from multiprocessing.pool import Pool > py> repr(Pool) > "" > > Seems pretty close to what you ask for. You can either pull that string > apart: > > py> s = repr(Pool) > py> start = s.find("'") > py> end = s.rfind("'") > py> s[start+1:end] > 'multiprocessing.pool.Pool' > > or you can construct it yourself: > > py> Pool.__module__ + '.' + Pool.__name__ > 'multiprocessing.pool.Pool' > > Yeah I considered doing it this way but was wary of that method because of possible changes to the implementation of the __repr__ method in the upstream code. If the Django developers don't consider the __repr__ method a public API then it could change in the future breaking my code. Of course this might not happen but I was hoping that there was a more generic way of doing it that did not rely on a certain implementation being in existence.