Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33248

Re: Getting module path string from a class instance

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!border2.nntp.ams2.giganews.com!border1.nntp.ams2.giganews.com!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!usenetcore.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <someukdeveloper@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
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; 'way:': 0.09; 'django': 0.10; 'ah,': 0.16; 'all?': 0.16; 'class;': 0.16; 'mistake.': 0.16; 'subject:class': 0.16; 'subject:instance': 0.16; 'wary': 0.16; 'string': 0.17; 'wrote:': 0.17; 'instance': 0.17; '>>>': 0.18; 'developer': 0.19; 'module': 0.19; 'code.': 0.20; 'changes': 0.20; 'trying': 0.21; 'import': 0.21; 'thanks.': 0.21; 'received:192.168.1.100': 0.22; 'class.': 0.23; 'this:': 0.23; 'seems': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'developers': 0.26; 'change,': 0.27; '>>>>': 0.29; "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; 'likely': 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; "didn't": 0.36; 'method': 0.36; 'possible': 0.37; 'does': 0.37; 'being': 0.37; 'detail': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'advice': 0.39; 'to:addr:python.org': 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; 'sounds': 0.71; 'hoping': 0.72; 'construct': 0.84; 'existence.': 0.84; 'upstream': 0.84; 'from.': 0.93
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=ObUwGtq5mIcvOmDiREejpKB3UUHFh+/VlAneW/Bn6u8=; b=kxse+YkXX+Cv9PyUEMJ1m114cWsyWmFB5Lniqjby1mzTpOzjlPI1XfxXzo51f2mvZP J7Ur/GJO5P3+Ccze8sdDyQAkSo7G6R6GdVdnH/FhDg1xWrIyj/h5rAQyRanrs2pd/mSU 5ueYAsCD5MG9zyL5sNMPR8RzUgNPLTTaCoTxotdqtodlG2cMVFfd0jKkSjsoyxTiaEJL YRu9Oxlg7FcVP9QHsEzEK9HyQBmR07StfGsNhKR98I3jdpgm4E8EG88HTXNP7sJA+YaL G/IM6cNCvWkVDd+hlF0vUIV9w9SlPkEx+H4dmyHa4cxX52wGQGun/vgtmMmDyFcH7n1W 2hSw==
Date Tue, 13 Nov 2012 16:15:13 +0000
From Some Developer <someukdeveloper@gmail.com>
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 <mailman.3611.1352788721.27098.python-list@python.org> <50a1f46e$0$21742$c3e8da3$76491128@news.astraweb.com> <mailman.3613.1352793277.27098.python-list@python.org> <50a209a6$0$21742$c3e8da3$76491128@news.astraweb.com>
In-Reply-To <50a209a6$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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3631.1352823318.27098.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352823318 news.xs4all.nl 6970 [2001:888:2000:d::a6]:55711
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33248

Show key headers only | View raw


On 13/11/2012 08:49, Steven D'Aprano wrote:
> On Tue, 13 Nov 2012 07:54:32 +0000, Some Developer wrote:
>
>> 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)
>>> "<class 'multiprocessing.pool.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.
> I didn't call SomeClass.__repr__. That is an implementation detail of
> SomeClass, and could change.
>
> I called repr(SomeClass), which calls the *metaclass* __repr__. That is
> less likely to change, although not impossible.
>
>
> If you're worried, just use the second way:
>
> SomeClass.__module__ + '.' + SomeClass.__name__
>   

Ah, my mistake. Thanks. That sounds exactly like what I want.

>> 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.
> SomeClass.__name__ is the official way to get the name of a class;
> SomeClass.__module__ is the official way to get the name of the module or
> package it comes from.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Getting module path string from a class instance Some Developer <someukdeveloper@gmail.com> - 2012-11-13 06:38 +0000
  Re: Getting module path string from a class instance Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-13 07:19 +0000
    Re: Getting module path string from a class instance Some Developer <someukdeveloper@gmail.com> - 2012-11-13 07:54 +0000
      Re: Getting module path string from a class instance Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-13 08:49 +0000
        Re: Getting module path string from a class instance Some Developer <someukdeveloper@gmail.com> - 2012-11-13 16:15 +0000

csiph-web