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


Groups > comp.lang.python > #59596

Re: Implementing #define macros similar to C on python

From Serhiy Storchaka <storchaka@gmail.com>
Subject Re: Implementing #define macros similar to C on python
Date 2013-11-16 12:02 +0200
References <fae7479b-ecec-4114-9750-6595fa8c78fa@googlegroups.com> <roy-2E4CE0.23100314112013@news.panix.com> <CAPTjJmrjkk=7+uvTq0JAjKgvY0Pri8O+1ouq9F0y0Br3r+2_2Q@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2705.1384596164.18130.python-list@python.org> (permalink)

Show all headers | View raw


15.11.13 06:57, Chris Angelico написав(ла):
> On Fri, Nov 15, 2013 at 3:10 PM, Roy Smith <roy@panix.com> 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 <iostream>
>
> 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

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


Thread

Implementing #define macros similar to C on python JL <lightaiyee@gmail.com> - 2013-11-14 18:29 -0800
  Re: Implementing #define macros similar to C on python Dave Angel <davea@davea.name> - 2013-11-14 21:30 -0600
  Re: Implementing #define macros similar to C on python Chris Angelico <rosuav@gmail.com> - 2013-11-15 14:49 +1100
    Re: Implementing #define macros similar to C on python JL <lightaiyee@gmail.com> - 2013-11-15 15:36 -0800
      Re: Implementing #define macros similar to C on python Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2013-11-16 01:20 +0100
      Re: Implementing #define macros similar to C on python Terry Reedy <tjreedy@udel.edu> - 2013-11-15 19:22 -0500
      Re: Implementing #define macros similar to C on python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-16 00:22 +0000
        Re: Implementing #define macros similar to C on python JL <lightaiyee@gmail.com> - 2013-11-15 21:38 -0800
          Re: Implementing #define macros similar to C on python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-16 11:48 +0000
  Re: Implementing #define macros similar to C on python Roy Smith <roy@panix.com> - 2013-11-14 23:10 -0500
    Re: Implementing #define macros similar to C on python Chris Angelico <rosuav@gmail.com> - 2013-11-15 15:57 +1100
    Re: Implementing #define macros similar to C on python Serhiy Storchaka <storchaka@gmail.com> - 2013-11-16 12:02 +0200
  Re: Implementing #define macros similar to C on python Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2013-11-16 00:20 +0100

csiph-web