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


Groups > comp.lang.python > #12760

Re: Need help with simple OOP Python question

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'instance': 0.05; 'subject:Python': 0.06; 'prints': 0.07; 'dict': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'variables.': 0.09; 'def': 0.15; 'a()': 0.16; 'instances.': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:OOP': 0.16; 'this:': 0.16; 'wrote:': 0.16; 'subject:question': 0.18; 'of.': 0.18; 'seems': 0.20; "haven't": 0.20; 'this?': 0.21; 'subject:help': 0.22; 'work,': 0.23; 'objects,': 0.23; 'code': 0.25; 'creating': 0.25; 'skip:b 20': 0.26; 'tried': 0.26; "i'm": 0.27; 'putting': 0.28; 'separate': 0.28; 'looks': 0.29; 'print': 0.29; 'from:addr:web.de': 0.30; 'sub': 0.30; 'class': 0.30; 'shared': 0.31; 'this.': 0.32; 'hi,': 0.32; 'objects': 0.32; 'to:addr:python-list': 0.33; "i've": 0.34; '...': 0.34; 'skip:- 40': 0.35; 'header:X-Complaints-To:1': 0.35; 'trouble': 0.35; 'but': 0.37; 'something': 0.37; 'two': 0.37; 'received:org': 0.38; 'some': 0.38; 'comments': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'subject:with': 0.39; 'why': 0.39; 'to:addr:python.org': 0.39; "it's": 0.40; 'your': 0.61; 'custom': 0.61; 'blank': 0.74; 'dict()': 0.84; 'unsuccessful': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Need help with simple OOP Python question
Date Mon, 05 Sep 2011 09:10:14 +0200
Organization None
References <dce02da0-c9c9-4331-aa2e-7d99f5e26cd1@g9g2000yqb.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p5084a72b.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.767.1315206626.27778.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1315206626 news.xs4all.nl 2493 [2001:888:2000:d::a6]:51721
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12760

Show key headers only | View raw


Kristofer Tengström wrote:

> Hi, I'm having trouble creating objects that in turn can have custom
> objects as variables. The code looks like this:
> 
> ---------------------------------------------
> 
> class A:
>     sub = dict()

Putting it into the class like this means sub is shared by all instances.

>     def sub_add(self, cls):
>         obj = cls()
>         self.sub[obj.id] = obj
> 
> class B(A):
>     id = 'inst'
> 
> base = A()
> base.sub_add(B)
> base.sub['inst'].sub_add(B)
> 
> print # prints a blank line
> print base.sub['inst']
> print base.sub['inst'].sub['inst']
> 
> ----------------------------------------------
> 
> Now, what I get from this is the following:
> <__main__.B instance at 0x01FC20A8>
> <__main__.B instance at 0x01FC20A8>
> Why is this? What I want is for them to be two separate objects, but
> it seems like they are the same one. I've tried very hard to get this
> to work, but as I've been unsuccessful I would really appreciate some
> comments on this. I'm sure it's something really easy that I just
> haven't thought of.

Your class A needs an initialiser:

class A:
    def __init__(self):
        self.sub = {} # one dict per instance
    # ...

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


Thread

Need help with simple OOP Python question Kristofer Tengström <krille012@gmail.com> - 2011-09-04 23:47 -0700
  Re: Need help with simple OOP Python question Stephen Hansen <me+list/python@ixokai.io> - 2011-09-05 00:07 -0700
  Re: Need help with simple OOP Python question Peter Otten <__peter__@web.de> - 2011-09-05 09:10 +0200
  Re: Need help with simple OOP Python question Ben Finney <ben+python@benfinney.id.au> - 2011-09-05 17:26 +1000
    Re: Need help with simple OOP Python question Kristofer Tengström <krille012@gmail.com> - 2011-09-05 06:15 -0700
      Re: Need help with simple OOP Python question Peter Otten <__peter__@web.de> - 2011-09-05 16:43 +0200
        Re: Need help with simple OOP Python question Jon Clements <joncle@googlemail.com> - 2011-09-05 07:59 -0700
          Re: Need help with simple OOP Python question Peter Otten <__peter__@web.de> - 2011-09-05 17:28 +0200
      Re: Need help with simple OOP Python question Terry Reedy <tjreedy@udel.edu> - 2011-09-05 13:38 -0400
        Re: Need help with simple OOP Python question Piet van Oostrum <piet@vanoostrum.org> - 2011-09-08 12:53 +0200

csiph-web