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


Groups > comp.lang.python > #54410

Re: mutlifile inheritance problem

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <nedbat@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; '"this': 0.03; 'argument': 0.05; '21,': 0.07; 'class,': 0.07; 'lines,': 0.07; '__init__': 0.09; 'defines': 0.09; 'instance.': 0.09; 'referenced': 0.09; 'sub': 0.09; 'url:blog': 0.10; 'python': 0.11; 'def': 0.12; 'windows': 0.15; '1.5.2': 0.16; 'a.py': 0.16; 'b.py': 0.16; 'item):': 0.16; 'parent:': 0.16; 'reason.': 0.16; 'typeerror:': 0.16; 'unbound': 0.16; 'thursday,': 0.16; 'sender:addr:gmail.com': 0.17; 'thanks,': 0.17; 'wrote:': 0.18; '(the': 0.22; 'import': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'instance,': 0.24; 'skip:i 40': 0.24; 'subject:problem': 0.24; '15,': 0.26; 'skip:" 40': 0.26; 'pass': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "i'm": 0.30; '"",': 0.31; 'marc': 0.31; 'restricted': 0.31; 'file': 0.32; 'class': 0.32; 'skip:c 30': 0.32; 'run': 0.32; 'running': 0.33; 'comment': 0.34; '"the': 0.34; 'skip:_ 10': 0.34; 'could': 0.34; 'problem': 0.35; "can't": 0.35; 'classes': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'skip:" 50': 0.36; 'doing': 0.36; 'method': 0.36; 'being': 0.38; 'to:addr:python- list': 0.38; 'files': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; 'read': 0.60; 'problems.': 0.60; 'march': 0.61; 'simply': 0.61; 'more': 0.64; 'different': 0.65; 'here': 0.66; 'skip:w 30': 0.69; '1st': 0.74; '2002': 0.81; 'topic,': 0.81; 'url:posts': 0.84; 'from.': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=dJfRPQK9RVKPD+N3IRat0nrz5T4GugtgHLD03Zh+cyM=; b=jHGgcR+2+pm0RF7yKPAzSqGItsXnyDJFDo48CI2cJc4a8fAd+MhG1E8x3PGp/1hN7c H77keOdeFB0Jqj0cEkZrYEZSuL3sz3GCAZwhDG0ny/UaFWPIM51tB2t55wZrpoYjO/qX 7fXv7lMsfVYhUBXynGt3HEZusDrDQCFWzmy/F6f6mf7d3N61spPCrxJBmOFYwVAzq7io fNHTT8jRxObyyr7iCpawVUwnp+86gacMCZ149ahU78pZNwo+JOhDSb91WOzczTAaDOl+ W1qHM5oezZfNTmtmEvSuw/ravpAoGJ6rNNx0BCyO8I63uT7snbZ5YnqWgauCgJbKxi+J 54fQ==
X-Received by 10.60.115.164 with SMTP id jp4mr12157859oeb.19.1379551093544; Wed, 18 Sep 2013 17:38:13 -0700 (PDT)
Sender Ned Batchelder <nedbat@gmail.com>
Date Wed, 18 Sep 2013 20:38:10 -0400
From Ned Batchelder <ned@nedbatchelder.com>
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130801 Thunderbird/17.0.8
MIME-Version 1.0
To python-list@python.org
Subject Re: mutlifile inheritance problem
References <9896e047.0203211303.741f695a@posting.google.com> <9043161b-e193-482a-a90c-42e8dfae978c@googlegroups.com>
In-Reply-To <9043161b-e193-482a-a90c-42e8dfae978c@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.143.1379551101.18130.python-list@python.org> (permalink)
Lines 136
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1379551101 news.xs4all.nl 15914 [2001:888:2000:d::a6]:48641
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54410

Show key headers only | View raw


On 9/18/13 7:54 PM, Peter Cacioppi wrote:
> This is a very old topic, but here is a trick for single inheritance. (The problem you allude to isn't restricted to multiple inheritance).
>
> Any class with a single parent simply defines this function.
>
>
>      def mySuper(self) :
>          return super(self.__class__, self)
>        
> And then any parent function be referenced like
>      self.mySuper().foo()

You can't use self.__class__ for super, it won't give you the right 
class if you are using subclasses:

     class MyBase(object):
         def mySuper(self):
             return super(self.__class__, self)

         def say(self):
             print "This is MyBase!"


     class Thing(MyBase):
         def say_it(self):
             print "The parent:"
             self.mySuper().say()

         def say(self):
             print "this is Thing!"

     class SubThing(Thing):
         pass

     thing = Thing()
     thing.say_it()

     sub = SubThing()
     sub.say_it()

This produces:

     The parent:
     This is MyBase!
     The parent:
     this is Thing!

super() takes a class and an instance for a reason. If you could use 
self.__class__ for the class, then it would only take the instance. 
Super() needs to know the instance, but also needs to know the class 
it's being called from.

--Ned.

> This includes __init__.
>
> You can read more here.
>
> http://atlee.ca/blog/posts/blog20081121python-reload-danger-here-be-dragons.html
>
>
> On Thursday, March 21, 2002 2:03:23 PM UTC-7, Marc wrote:
>> I have classes defined in different files and would like to inherit
>> from a class in file A.py for a class in file B.py but am running into
>> problems.  I'm using Python 1.5.2 on Windows NT
>>
>> Here's a specific example:
>>
>> ************************
>> file cbase01.py:
>>
>> class CBase:
>>
>>      def __init__(self):
>>          self.cclass = None
>>          print "cbase"
>>
>> class CImStream(CBase):
>>
>>      def __init(self):
>>          CBase.__init__(self)
>>          print "CImStream"
>>
>> *************************
>> in file wrappers_A01.py:
>>
>> import cbase01
>> reload(cbase01)
>>
>> class ImStream_SavedBitmaps(cbase01.CImStream):
>>
>>      def __init__(self):
>>          cbase.CImStream.__init__(self)
>>          print "SavedBitmaps"
>>
>> **************************
>> in file sequencer01.py
>>
>> import cbase01    # the offending lines, program works
>> reload(cbase01)   # if I comment these out.
>>
>> class Sequencer:
>>
>>      def Append(self, item):
>>          pass
>>
>> *****************************
>> in test02.py
>>
>> import wrappers_A01
>> reload(wrappers_A01)
>>
>> import sequencer01
>> reload(sequencer01)
>>
>> x0 = wrappers_A01.ImStream_SavedBitmaps()
>> ***************************************************************
>>
>> If I run test02 I get the traceback
>>
>> Traceback (innermost last):
>>    File "<string>", line 1, in ?
>>    File "D:\PythonCode\pna\eyeTracking\tests\test02.py", line 15, in ?
>>      x0 = wrappers_A01.ImStream_SavedBitmaps()
>>    File "D:\PythonCode\pna\eyeTracking\tests\wrappers_A01.py", line 21,
>> in __init__
>>      cbase.CImStream.__init__(self)
>> TypeError: unbound method must be called with class instance 1st
>> argument
>>
>>
>> Any ideas what I am doing wrong?
>>
>> Thanks,
>> Marc

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


Thread

Re: mutlifile inheritance problem Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-09-18 16:54 -0700
  Re: mutlifile inheritance problem Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-09-18 17:03 -0700
  Re: mutlifile inheritance problem Ned Batchelder <ned@nedbatchelder.com> - 2013-09-18 20:38 -0400
    Re: mutlifile inheritance problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-19 02:05 +0000

csiph-web