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


Groups > comp.lang.python > #52822

Re: Basic Python Query

References <kv32q9$rd2$1@news.albasani.net> <1377158037.22487.YahooMailBasic@web190503.mail.sg3.yahoo.com>
Date 2013-08-22 09:46 +0100
Subject Re: Basic Python Query
From Fábio Santos <fabiosantosart@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.125.1377161201.19984.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On 22 Aug 2013 08:58, "chandan kumar" <chandan_psr@yahoo.co.in> wrote:
>
> Hi all,
>
> Sorry for not explaining question properly.Here Its not about threading
and dont worry about any indentations.Please see below example
>
> class Call_Constructor():
>     def __init__(self):
>         print "In __init__ "
>
> class Test_Constructor(Call_Constructor):
>     def method(self):
>        print " In Test_Constructor Class"
>
>   ConstructInstance = Test_Constructor()
>
> When an instace is created for Test_Constructor class ,The code flows
starts __init__ in Call_Constructor class.Whys is it like that.
>
>
> class Call_Constructor():
>     def __init__(self):
>         print "In __init__ "
>
>
> class Test_Constructor(Call_Constructor):
>     def  __init__(self):
>        print " In Test_Constructor Class"
>
>   ConstructInstance = Test_Constructor()
>
> But for the above case Code flows starts from Test_Constructor().
>
> Whats is the difference for both cases described above.
>
> Best Regards,
> Chandan.

When creating an instance, Python will call __init__. In the first example
there was only an __init__ method in the base class, so that one was used.
On the second example, there were __init__ methods on both classes, but
since you instantiated the subclass, the subclass's __init__ method was
executed.

Subclass methods have precedence over base class methods. If you want the
__init__ method of the base class in the second example to be called, you
can either remove the subclass __init__ method or call
super(TestConstructor, self).__init__() in that method. That will call the
base class's __init__.

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


Thread

Basic Python Query chandan kumar <chandan_psr@yahoo.co.in> - 2013-08-21 14:50 +0800
  Re: Basic Python Query Steven D'Aprano <steve@pearwood.info> - 2013-08-21 08:19 +0000
  Re: Basic Python Query Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-08-21 11:11 +0200
    Re: Basic Python Query Johannes Bauer <dfnsonfsduifb@gmx.de> - 2013-08-21 20:58 +0200
      Re: Basic Python Query Fábio Santos <fabiosantosart@gmail.com> - 2013-08-21 23:50 +0100
        Re: Basic Python Query Johannes Bauer <dfnsonfsduifb@gmx.de> - 2013-08-22 10:49 +0200
      Re: Basic Python Query Ned Batchelder <ned@nedbatchelder.com> - 2013-08-21 20:06 -0400
        Re: Basic Python Query Bob Martin <bob.martin@excite.com> - 2013-08-22 06:43 +0100
          Re: Basic Python Query Ned Batchelder <ned@nedbatchelder.com> - 2013-08-22 09:45 -0400
            Re: Basic Python Query Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-23 03:12 +0000
          Re: Basic Python Query random832@fastmail.us - 2013-08-22 14:57 -0400
          Re: Basic Python Query Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-08-22 18:22 -0400
          Re: Basic Python Query random832@fastmail.us - 2013-08-23 01:08 -0400
        Re: Basic Python Query Johannes Bauer <dfnsonfsduifb@gmx.de> - 2013-08-22 10:50 +0200
      Re: Basic Python Query Fábio Santos <fabiosantosart@gmail.com> - 2013-08-22 09:46 +0100
      Re: Basic Python Query Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-08-22 13:54 +0200
        Re: Basic Python Query Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-23 03:28 +0000
          Re: Basic Python Query Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-08-23 09:12 +0200
            Re: Basic Python Query Chris Angelico <rosuav@gmail.com> - 2013-08-24 01:50 +1000

csiph-web