Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attribute': 0.05; 'case.': 0.05; 'class,': 0.07; 'attribute.': 0.09; 'instance.': 0.09; 'subject:Getting': 0.09; 'subject:module': 0.09; 'subject:string': 0.09; 'stored': 0.10; 'all?': 0.16; 'subject:class': 0.16; 'subject:instance': 0.16; 'string': 0.17; 'wrote:': 0.17; 'basically': 0.17; 'have:': 0.17; 'instance': 0.17; 'package.': 0.17; 'skip:i 40': 0.17; 'string,': 0.17; 'developer': 0.19; 'module': 0.19; 'trying': 0.21; 'import': 0.21; 'received:192.168.1.100': 0.22; 'help.': 0.22; 'class.': 0.23; 'this:': 0.23; 'idea': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'starts': 0.29; 'class': 0.29; "i'm": 0.29; 'received:209.85.215.46': 0.30; 'figure': 0.30; 'code': 0.31; 'asking': 0.32; 'could': 0.32; 'print': 0.32; 'certain': 0.33; 'anyone': 0.33; 'to:addr:python- list': 0.33; 'equal': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'path': 0.35; 'stores': 0.35; 'received:209.85': 0.35; 'really': 0.36; 'but': 0.36; 'data.': 0.36; 'loaded': 0.36; 'modules': 0.36; 'method': 0.36; 'display': 0.36; 'does': 0.37; 'why': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'there,': 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; 'information': 0.63; 'offer': 0.65; 'angel': 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=ozJc/IdJLYUak0l7ncybiUb+tjQ7On0ZeMIBMZT9ll4=; b=hwDFaBIhJbw7/qW8rmrCguIc1bQ8ciyjBLLj/NOiQPZg7nlnfJk37AdrEAEPK8vjfo I2e162SWR6t4BnVayEut47HN3nvKDXYU5dUT6deh8uHU3KCfHcVeOVRUB/lW2Zn4sdji 8bdmxbJpyByj7+gw2NiizPCDyooNZEI9Oeau+t3HS/E+YSIYv2vxdaAOEO4FbIIQyooN fxJJF0BRs/FpNUnRAQ8QDQgfUlu0inczRDGp4Rc+Xez7OYF5TEWsQEdQDWcDgtaEqdDB ZIkQIfLeBbORRmCbMxwHzDvO0fyhZWXp51huQGXqj/RyJMEayIeRFAsnEBLOmM1bOEK3 uc6w== Date: Tue, 13 Nov 2012 16:23:12 +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: <50A1EAE7.1090700@googlemail.com> <50A2149E.303@davea.name> In-Reply-To: <50A2149E.303@davea.name> 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: 1352823797 news.xs4all.nl 6954 [2001:888:2000:d::a6]:34092 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33250 On 13/11/2012 09:36, Dave Angel wrote: > On 11/13/2012 01:38 AM, 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? > So you have: > > import my.module > theclass = my.module.Foo > print tellme(theClass) > > and you want to know how to write tellme? Why not just change it to > take a string, and pass it "my.module.Foo" ? > > If you have stored the class away somewhere, and want to figure it out > from there, you could look at the __module__ attribute. that'll tell > you the module name, but not the class name. > > > If you're really asking how to get that string from an INSTANCE of the > class, then try the __class__ attribute of that instance. I'm actually writing a dynamic importer. Basically when the program starts it queries all the loaded modules for a certain package. It then stores the module and class information in a database and then when a certain view is called the specific data required is loaded by calling importlib.import_module('module.class'). This then populates the view with the required data. Basically the idea is to be able to display any data required without having to tightly couple the view code to a specific model type. Thanks for the help.