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


Groups > comp.lang.python > #21462

Re: stackoverflow question

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <tjreedy@udel.edu>
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; 'attributes': 0.05; 'attribute': 0.07; 'raises': 0.07; 'received:verizon.net': 0.07; 'stuff,': 0.07; 'terry': 0.07; '(there': 0.09; 'created,': 0.09; 'header:In-reply-to:1': 0.09; 'module)': 0.09; 'received:206.46': 0.09; 'received:206.46.173': 0.09; 'output': 0.10; 'am,': 0.12; 'def': 0.13; 'metaclass': 0.16; 'reedy': 0.16; 'stringio': 0.16; 'sys.stdout': 0.16; 'cc:addr:python-list': 0.16; 'subject:question': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'instance': 0.18; 'subject:skip:s 10': 0.18; 'jan': 0.19; 'cc:no real name:2**0': 0.21; 'tracker': 0.21; 'cc:2**0': 0.26; 'code': 0.26; 'import': 0.27; "i'm": 0.28; 'class': 0.29; 'cc:addr:python.org': 0.29; '(and': 0.30; 'lines': 0.30; 'object.': 0.30; 'received:192.168.1.3': 0.30; '---': 0.31; 'version': 0.32; 'capture': 0.32; 'does': 0.32; 'error.': 0.32; 'objects': 0.32; 'idea': 0.32; "isn't": 0.33; "won't": 0.33; 'header:User-Agent:1': 0.33; 'object': 0.33; 'normally': 0.34; 'certain': 0.34; 'setting': 0.34; 'anything': 0.34; 'issue': 0.37; 'created': 0.37; 'run': 0.37; 'but': 0.37; 'received:192': 0.38; 'some': 0.38; 'doing': 0.38; 'received:192.168.1': 0.39; 'processing': 0.39; '(1)': 0.40; 'target': 0.63; '11:56': 0.84
Date Sat, 10 Mar 2012 13:00:03 -0500
From Terry Reedy <tjreedy@udel.edu>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0
MIME-version 1.0
To Ethan Furman <ethan@stoneleaf.us>
Subject Re: stackoverflow question
References <4F5A7FCA.6030109@stoneleaf.us> <jje312$h1p$1@dough.gmane.org> <4F5B87DA.5040401@stoneleaf.us>
In-reply-to <4F5B87DA.5040401@stoneleaf.us>
Content-type text/plain; charset=UTF-8; format=flowed
Content-transfer-encoding 7bit
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.549.1331403637.3037.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1331403637 news.xs4all.nl 6930 [2001:888:2000:d::a6]:38570
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21462

Show key headers only | View raw


On 3/10/2012 11:56 AM, Ethan Furman wrote:

>>> I'm writing a metaclass to do some cool stuff, and part of its
>>> processing is to check that certain attributes exist when the class is
>>> created. Some of these are mutable, and would normally be set in
>>> `__init__`, but since `__init__` isn't run until the instance is created
>>> the metaclass won't know that the attribute *will* be created, and
>>> raises an error.

>> c. Check the code object to see if the attribute will be created.
>
> I have no idea how to do this -- pointers?

Capture output of dis.dis(targetclass.__init__) by temporarily setting 
sys.stdout to StringIO object. (There is tracker issue to make lines 
available to a program without doing this.)

 >>> from dis import dis
 >>> def f(self):
	self.attr = 1
	
 >>> dis(f)
   2           0 LOAD_CONST               1 (1)
               3 LOAD_FAST                0 (self)
               6 STORE_ATTR               0 (attr)
               9 LOAD_CONST               0 (None)
              12 RETURN_VALUE

Look for STORE_ATTR line with target attribute name. If you want to 
check for wacko code that does setattr(self, 'attr', value), try it in 
'f' and dis again.

Anything to do with code objects (and dis module) is implementation and 
version specific

---
Terry Jan Reedy

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


Thread

Re: stackoverflow question Terry Reedy <tjreedy@udel.edu> - 2012-03-10 13:00 -0500

csiph-web