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


Groups > comp.lang.python > #107676

Re: def __init__(self):

From Chris Kaynor <ckaynor@zindagigames.com>
Newsgroups comp.lang.python
Subject Re: def __init__(self):
Date 2016-04-26 10:13 -0700
Message-ID <mailman.118.1461690843.32212.python-list@python.org> (permalink)
References (7 earlier) <mailman.114.1461687970.32212.python-list@python.org> <571f9836$0$1619$c3e8da3$5496439d@news.astraweb.com> <CALvWhxtRM+Qd983o2eB2SC4Hk0EsU26t-zbOQFW23a27Z3gx3Q@mail.gmail.com> <CAPTjJmoaJWHaXz_6jVoGzCm9Y4C+b5a=4C8gHoxt9B=jxQpJGQ@mail.gmail.com> <CALvWhxs4wk0c=PQrB7E6+KVQ5=mgo2At7HSP3_5v39t1jgeprw@mail.gmail.com>

Show all headers | View raw


On Tue, Apr 26, 2016 at 10:04 AM, Chris Angelico <rosuav@gmail.com> wrote:

> On Wed, Apr 27, 2016 at 2:59 AM, Chris Kaynor <ckaynor@zindagigames.com>
> wrote:
> > On Tue, Apr 26, 2016 at 9:32 AM, Steven D'Aprano <steve@pearwood.info>
> > wrote:
> >
> >> Subclassing immutable built-ins is the most obvious and simple (and
> >> probably
> >> common) way to get an immutable class. Actually immutable, short of
> doing
> >> wicked things with ctypes.
> >>
> >
> > By wicked things with ctypes, do you mean something like this? By no
> means
> > do I suggest this actually be used by anybody for any reason.
> >
> > Tested with '2.7.10 (default, Jul 14 2015, 19:46:27) \n[GCC 4.2.1
> > Compatible Apple LLVM 6.0 (clang-600.0.39)]'
> >
> > import ctypes
> > def changeTuple(tuple, index, newValue):
> >     obj = ctypes.cast(id(tuple), ctypes.POINTER(ctypes.c_long))
> >     obj[3+index] = id(newValue)
> >
> >>>> a = ('a','b','c')
> >>>> changeTuple(a, 0, 1)
> >>>> a
> > (1, 'b', 'c')
> >>>> changeTuple(a, 1, 3)
> >>>> a
> > (1, 3, 'c')
>
> Yeah. By the look of things, you've just destroyed the reference counts.
>
> >>> a = ('a','b','c')
> >>> b = object()
> >>> changeTuple(a, 0, b)
> >>> a
> (<object object at 0x7f1240b22080>, 'b', 'c')
> >>> del b
> >>> a
> Segmentation fault
>

Yah, if you really wanted to make it work properly, you'd need to incref
the newValue, while decref the oldValue. The incref would not be that
difficult, but the decref would be more challenging, as you may have to
also destroy the old object, though that might be possible by casting it
back to a python object without the incref,. One way or the other, I did
not exactly spend a ton of time to make it work properly :)

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


Thread

def __init__(self): San <santanu01@gmail.com> - 2016-04-25 23:21 -0700
  Re: def __init__(self): Ben Finney <ben+python@benfinney.id.au> - 2016-04-26 16:31 +1000
  Re: def __init__(self): Gary Herron <gherron@digipen.edu> - 2016-04-26 00:06 -0700
  Re: def __init__(self): Ben Finney <ben+python@benfinney.id.au> - 2016-04-26 17:34 +1000
    Re: def __init__(self): Marko Rauhamaa <marko@pacujo.net> - 2016-04-26 11:25 +0300
      Re: def __init__(self): Steven D'Aprano <steve@pearwood.info> - 2016-04-27 02:12 +1000
        Re: def __init__(self): Random832 <random832@fastmail.com> - 2016-04-26 12:26 -0400
          Re: def __init__(self): Steven D'Aprano <steve@pearwood.info> - 2016-04-27 02:32 +1000
            Re: def __init__(self): Chris Kaynor <ckaynor@zindagigames.com> - 2016-04-26 09:59 -0700
              Re: def __init__(self): Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-04-27 14:54 +1000
            Re: def __init__(self): Chris Angelico <rosuav@gmail.com> - 2016-04-27 03:04 +1000
            Re: def __init__(self): Chris Kaynor <ckaynor@zindagigames.com> - 2016-04-26 10:13 -0700
            Re: def __init__(self): Ian Kelly <ian.g.kelly@gmail.com> - 2016-04-26 11:26 -0600
            Re: def __init__(self): Chris Angelico <rosuav@gmail.com> - 2016-04-27 03:30 +1000
        Re: def __init__(self): Marko Rauhamaa <marko@pacujo.net> - 2016-04-26 19:38 +0300
  Re: def __init__(self): Random832 <random832@fastmail.com> - 2016-04-26 09:49 -0400
  Re: def __init__(self): Gary Herron <gherron@digipen.edu> - 2016-04-26 07:25 -0700

csiph-web