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


Groups > comp.lang.python > #19565

object aware of others

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed20.multikabel.net!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <lchaplin13@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'attributes': 0.05; 'definitions': 0.07; 'subject:object': 0.07; "'this": 0.09; 'presume': 0.09; 'def': 0.13; 'a()': 0.16; 'b()': 0.16; 'received:209.85.161.174': 0.16; 'received:mail- gx0-f174.google.com': 0.16; 'thanks,': 0.18; 'trying': 0.20; "doesn't": 0.22; 'found,': 0.23; 'itself,': 0.23; 'module,': 0.23; 'module': 0.26; '"the': 0.26; 'all,': 0.27; 'import': 0.27; 'lee': 0.28; 'message-id:@mail.gmail.com': 0.28; 'print': 0.29; 'class': 0.29; 'lines': 0.30; "i've": 0.31; 'objects': 0.32; 'to:addr :python-list': 0.33; 'object': 0.33; 'there': 0.33; 'received:209.85.161': 0.34; 'something': 0.35; 'but': 0.37; 'received:google.com': 0.37; 'skip:_ 10': 0.37; 'replace': 0.38; 'some': 0.38; 'received:209.85': 0.38; 'created': 0.38; 'appreciated.': 0.39; 'help': 0.39; 'called': 0.39; 'received:209': 0.39; 'greatly': 0.39; 'to:addr:python.org': 0.40; 'got': 0.40; 'friends': 0.66; '"foo"': 0.84; 'subject:others': 0.84; 'what.': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; bh=2xYnXnCsBPF1smG8qe2lyZy2QFd8jFlv/OFNOZuuuyw=; b=dD4jj7Cq8tOC0PR2+KrFjjVGIS0sirwAR8Q+eRqJv5RPMPeO3yGpVTwpFT4mwWicnq loRfGKaxRh+JqZTBFaxfFItIedqOHerQFYFAJEOOX+cmmKPgCqMl9UKvDTFmer8hRxqs 1rJEKcpCD+RL7Cl9mnp1atZ8cUG0FLIkijCHg=
MIME-Version 1.0
Date Sun, 29 Jan 2012 16:48:34 +1300
Subject object aware of others
From Lee Chaplin <lchaplin13@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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.5187.1327808922.27778.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1327808922 news.xs4all.nl 6930 [2001:888:2000:d::a6]:52579
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19565

Show key headers only | View raw


Hi all,

I am trying to create an object that is aware of other objects created
before itself, and when found, then copy some attributes from them,
something like:

class A:
    def __init__(self):
        self.myname = "IamA"
        print 'This is A'
    def foo(self):
        print "foo"
    def update(self):
        i = ''
        obj = self
        for i in globals():
            obj = globals()[i]
            if hasattr(obj, 'myname'):
                print "The only friends I've got are ", i, obj.myname
            else:
                print "Oops, not my friend."


class B:
    def __init__(self):
        print 'This is B'
    def foo(self):
        print "bar"

# a = A()
# b = B()
# c = A()
# c.update()

The last four lines work if they are in the same module as the class
definitions (a000), but it doesn't work if they are called from a
different module, say:

import a000

a = a000.A()
b = a000.B()
c = a000.A()
c.update()

I presume there is something that need to replace the globals() call,
but I cannot find what.
Any help is greatly appreciated.

Thanks,
Lee

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


Thread

object aware of others Lee Chaplin <lchaplin13@gmail.com> - 2012-01-29 16:48 +1300
  Re: object aware of others Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-29 04:13 +0000

csiph-web