Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'float': 0.07; '#define': 0.09; '#include': 0.09; '0.1': 0.09; 'main()': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'builtins': 0.16; 'c/c++': 0.16; 'inaccurate': 0.16; 'other)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'roy': 0.16; 'value;': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'import': 0.22; 'header:User- Agent:1': 0.23; 'header': 0.24; '15,': 0.26; '2.0': 0.26; 'this:': 0.26; 'somewhere': 0.26; 'header:X-Complaints-To:1': 0.27; 'header :In-Reply-To:1': 0.27; 'chris': 0.29; 'to?': 0.30; 'struct': 0.31; 'file': 0.32; 'class': 0.32; 'fri,': 0.33; 'totally': 0.33; 'skip:_ 10': 0.34; 'should': 0.36; 'application': 0.37; '8bit%:86': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'most': 0.60; 'received:46': 0.66; 'smith': 0.68; 'hey,': 0.75; 'power': 0.76; '"look': 0.84; 'horrible': 0.84; 'subject: #': 0.96; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Serhiy Storchaka Subject: Re: Implementing #define macros similar to C on python Date: Sat, 16 Nov 2013 12:02:43 +0200 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: 46.211.124.20 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 In-Reply-To: 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384596164 news.xs4all.nl 15969 [2001:888:2000:d::a6]:46126 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59596 15.11.13 06:57, Chris Angelico написав(ла): > On Fri, Nov 15, 2013 at 3:10 PM, Roy Smith wrote: >> Why would you want to? One of the most horrible things about C/C++ is >> the preprocessor. > > Hey, that's not fair! Without the preprocessor, how would you be able > to do this: > > //Hide this part away in a header file somewhere > struct b0rkb0rk > { > float value; > b0rkb0rk(float v):value(v) {} > operator float() {return value;} > float operator +(float other) {return value+other-0.1;} > }; > //Behold the power of the preprocessor! > #define float b0rkb0rk > > //Okay, now here's your application > #include > > int main() > { > std::cout << "Look how stupidly inaccurate float is!\n"; > float x = 123.0f; > std::cout << "123.0 + 2.0 = " << x + 2.0f << "\n"; > std::cout << "See? You should totally use double instead.\n"; > } > > (Anybody got a cheek de-tonguer handy? I think it's stuck.) >>> class b0rkb0rk(float): ... def __add__(self, other): ... return super().__add__(other) - 0.1 ... >>> import builtins >>> builtins.float = b0rkb0rk >>> float(123) + 2 124.9