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


Groups > comp.lang.python > #90566

Re: Basic misunderstanding on object creation

Newsgroups comp.lang.python
Date 2015-05-13 08:52 -0700
References <25ba3a96-21ee-4a83-b7c1-8ac60508d30c@googlegroups.com> <mivmr0$1mb$1@r01.glglgl.de> <06301cb8-39a6-410b-9fad-afe6bd4d3217@googlegroups.com> <e09c84fe-609c-4794-a117-fc72c8ccf3ad@googlegroups.com> <mailman.448.1431528969.12865.python-list@python.org>
Message-ID <66066418-2b01-44a2-a551-1d822af53a3b@googlegroups.com> (permalink)
Subject Re: Basic misunderstanding on object creation
From andrew cooke <andrew@acooke.org>

Show all headers | View raw


On Wednesday, 13 May 2015 11:56:21 UTC-3, Ian  wrote:
> On Wed, May 13, 2015 at 8:45 AM, andrew cooke <andrew@acooke.org> wrote:
> >>>> class Foo:
> > ...     def __new__(cls, *args, **kargs):
> > ...         print('new', args, kargs)
> > ...         super().__new__(cls)
> > ...
> >>>> class Bar(Foo):
> > ...     def __init__(self, a):
> > ...         print('init', a)
> > ...
> >>>> Bar(1)
> > new (1,) {}
> >
> > no "init" is printed.
> 
> You're not returning anything from Foo.__new__, so the result of the
> constructor is None.  None.__init__ does nothing.

ah, you're right, thanks.  that was a typo.


more generally, it seems that the error is:

  (1) __new__ is called and then __init__ from some other, external code

  (2) in 3.2 anything passed to object's __new__ was silently discarded.

the following code works in 3.2 and 3.4:

class Foo:
    def __new__(cls, *args, **kargs):
        print("new", args, kargs)
        return super().__new__(cls)

class Bar(Foo): 
    def __init__(self, *args, **kargs):
        print("init", args, kargs)

Bar(1)

(while my original code didn't).

thanks everyone,
andrew

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


Thread

Basic misunderstanding on object creation andrew cooke <andrew@acooke.org> - 2015-05-13 06:25 -0700
  Re: Basic misunderstanding on object creation Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2015-05-13 16:24 +0200
    Re: Basic misunderstanding on object creation andrew cooke <andrew@acooke.org> - 2015-05-13 07:42 -0700
      Re: Basic misunderstanding on object creation andrew cooke <andrew@acooke.org> - 2015-05-13 07:45 -0700
        Re: Basic misunderstanding on object creation Peter Otten <__peter__@web.de> - 2015-05-13 16:55 +0200
        Re: Basic misunderstanding on object creation Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-13 08:55 -0600
          Re: Basic misunderstanding on object creation andrew cooke <andrew@acooke.org> - 2015-05-13 08:52 -0700
      Re: Basic misunderstanding on object creation Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-13 08:54 -0600
  Re: Basic misunderstanding on object creation Terry Reedy <tjreedy@udel.edu> - 2015-05-13 12:36 -0400
    Re: Basic misunderstanding on object creation andrew cooke <andrew@acooke.org> - 2015-05-13 11:42 -0700
      Re: Basic misunderstanding on object creation Terry Reedy <tjreedy@udel.edu> - 2015-05-13 15:17 -0400
      Re: Basic misunderstanding on object creation Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-05-13 20:45 +0100
        Re: Basic misunderstanding on object creation Ned Batchelder <ned@nedbatchelder.com> - 2015-05-13 13:33 -0700
          Re: Basic misunderstanding on object creation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-14 11:07 +1000
  Re: Basic misunderstanding on object creation Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-05-13 17:38 +0100
  Re: Basic misunderstanding on object creation Terry Reedy <tjreedy@udel.edu> - 2015-05-13 13:05 -0400
  Re: Basic misunderstanding on object creation Terry Reedy <tjreedy@udel.edu> - 2015-05-13 13:13 -0400
  Re: Basic misunderstanding on object creation Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-05-13 18:43 +0100

csiph-web