News for hackers. Stuff that matters.
Re:[Python-ideas] List Revolution by pyre on Saturday September 10, 2011 @02:04AM (#2980973)
Good news! You can have this right now in Perl:

  $[ = 1;
Now all of your arrays are 1-indexed!
Re:[Python-ideas] List Revolution by vessenes on Saturday September 10, 2011 @02:45AM (#2981028)
I am truly, truly horrified that this a language option in perl, although I'm not surprised. (Well, I did google it to see if you were joking, but you weren't, apparently).

The older I get the more Larry Wall strikes me as like a really well intentioned hippy type; when in God's green earth has it been a good idea to allow a five character statement change the truth values of nearly every for loop in a body of code?

Perl always claimed it gave you enough rope to hang yourself, but this goes far, far beyond that. This is like building the scaffolding and marking it like "Young developers, play here! Ropes are fun!"

Re:[Python-ideas] List Revolution by pyre on Saturday September 10, 2011 @03:16AM (#2981069)
1) It's not like the docs don't recommend against it.

2) As of Perl 5+ it's evaluated at compile-time rather than run-time, which is not as bad as if you could flip back and forth at runtime. Though you can use 'local' to lexically scope its effects -- hilarity ensues as you hand off said code to someone else for maintenance.

3) It's from a different era. As per the docs:

    Default is 0, but you could theoretically set
    it to 1 to make Perl behave more like awk (or
    Fortran)
It's there to help you emulate awk or Fortran... definitely targeted at today's young programmers. ;-)

[ Though the core of SciPy/NumPy is notably written in Fortran. Not quite as dead as some would have you believe. ]

Re:[Python-ideas] List Revolution by irahul on Saturday September 10, 2011 @02:52AM (#2981038)
> I am truly, truly horrified that this a language option in perl, although I'm not surprised.

How about this in C(and thousand others): #define if while

I am pretty sure I can find something to horrify you in almost any language.

Re:[Python-ideas] List Revolution by Jach on Saturday September 10, 2011 @07:40AM (#2981397)
Let's throw in a fun Python hack too:

    >>> import sys
    >>> import ctypes
    >>> pyint_p = ctypes.POINTER(ctypes.c_byte*sys.getsizeof(5))
    >>> five = ctypes.cast(id(5), pyint_p)
    >>> 2 + 2 == 5
    False
    >>> five.contents[five.contents[:].index(5)] = 4
    >>> 2 + 2 == 5
    True
Re:[Python-ideas] List Revolution by phzbOx on Saturday September 10, 2011 @09:03AM (#2981504)
May I ask how did you find about that? Sometime I would like to play in ctypes but I'm not sure where to start :-/

Here's a small snippet that let you add methods to built-ins:

  def inject(cls, wrapper=lambda x: x, name=None):
      def _builtin_hack(name):
          import ctypes as c
  
          _get_dict = c.pythonapi._PyObject_GetDictPtr
          _get_dict.restype = c.POINTER(c.py_object)
          _get_dict.argtypes = [c.py_object]
  
          return _get_dict(name).contents.value
  
      def wrap(method):
          name_ = name or method.func_name
          method = wrapper(method)
  
          try:
              setattr(cls, name_, method)
          except:
              _builtin_hack(cls)[name_] = method
  
      return wrap  


    @inject(dict)
    def extend(self, new_dict):
        return dict(self, **new_dict)

    print {'a':2}.extend({'b':3})
Re:[Python-ideas] List Revolution by Jach on Saturday September 10, 2011 @08:19PM (#2982867)
ctypes is like god-mode for Python... I like your hack too. I just play around with it from time to time, I believe the only "serious" thing I've done with it so far is making a Python interface around a C library I made.

I think I discovered the hack when I found out about the id() function, and hex(id(5)) looked suspiciously like a pointer value. (And http://docs.python.org/library/functions.html#id confirms.) http://docs.python.org/library/ctypes.html has usually been pretty useful. Apart from that, just play.

Re:[Python-ideas] List Revolution by MostAwesomeDude on Saturday September 10, 2011 @03:26PM (#2982268)
You can do anything in ctypes, though. :3

    $ python -c "import ctypes; ctypes.memset(0, 0, 42)"
    Segmentation fault
Re:[Python-ideas] List Revolution by Sharlin on Saturday September 10, 2011 @09:00AM (#2981499)
#defining reserved words is not permitted by standard C (or C++), although compilers do allow it in practice. Anyway, it's a totally different thing to abuse a general-purpose feature to do horrible thing X than to have a specific feature, only useful for doing horrible things, built into the language.
Re:[Python-ideas] List Revolution by pyre on Saturday September 10, 2011 @03:19AM (#2981071)
Or more subtly:

  #define break continue
Or Ruby:

  irb(main):001:0> if 0
  irb(main):002:1> print "True!"
  irb(main):003:1> end
  True!=> nil
(Though the Ruby example probably not as horrific as the others)
Re:[Python-ideas] List Revolution by rat on Saturday September 10, 2011 @05:46AM (#2981265)
true the ruby should be a type error
Re:[Python-ideas] List Revolution by chromatic on Saturday September 10, 2011 @03:24AM (#2981077)
It's deprecated in modern releases of Perl 5 and shall likely be removed.
Re:[Python-ideas] List Revolution by shuzchen on Saturday September 10, 2011 @02:09AM (#2980980)
Lua does this already. And that's the part about Lua that I don't like. But that's mostly my personal preference speaking.