Deutsch   English   Français   Italiano  
<ast-20240110203859@ram.dialup.fu-berlin.de>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: extend behaviour of assignment operator
Date: 10 Jan 2024 19:40:49 GMT
Organization: Stefan Ram
Lines: 25
Expires: 1 Dec 2024 11:59:58 GMT
Message-ID: <ast-20240110203859@ram.dialup.fu-berlin.de>
References: <mailman.1.1704895573.15798.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de +Pf7wNw/likkiD+6EA6T6wFVVlKbjly4xtrKt6iZZmAPX7
Cancel-Lock: sha1:5mbNTkq1fsL6LI0qgVzSQ83aoMI= sha256:adftsrYxZ5bpmhBcwzaSanHH1mK5DQ7DLVSUYYLXFOs=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
	Distribution through any means other than regular usenet
	channels is forbidden. It is forbidden to publish this
	article in the Web, to change URIs of this article into links,
        and to transfer the body without this notice, but quotations
        of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
	services to mirror the article in the web. But the article may
	be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE-1901, en-US, it, fr-FR

Guenther Sohler <guenther.sohler@gmail.com> writes:
>i'd like to extend the behaviour  of the assignment operator

  The following attempt is just a proof of concept, as it will
  not handle properly all possible code. To do something similar
  in a more general way, use "import ast", parse the code into
  an AST and then search the AST for assignements and insert
  code to change the name attribute after them.

import re

class thing: pass # all things pass

code = r'''
a = thing()
print( a.name )
b = a
print( b.name )
'''

code = \
re.sub\
( r'((?:^|\n)(\s*)([\w_]+)\s*=.*(\n|$))', r'\1\2\3.name="\3"\n', code )
exec( code )