Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #12762
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Need help with simple OOP Python question |
| References | <dce02da0-c9c9-4331-aa2e-7d99f5e26cd1@g9g2000yqb.googlegroups.com> |
| Date | 2011-09-05 17:26 +1000 |
| Message-ID | <87sjobo3es.fsf@benfinney.id.au> (permalink) |
| Organization | Unlimited download news at news.astraweb.com |
Kristofer Tengström <krille012@gmail.com> writes: > Hi, I'm having trouble creating objects that in turn can have custom > objects as variables. That terminology is rather confused. I think what you want is to have instances with their own attributes. > class A: > sub = dict() This binds a single object (a new empty dict) to the class attribute ‘sub’. Every instance of class ‘A’ will share the same attribute, and hence that same dict. > def sub_add(self, cls): This defines a function which will be bound to the class attribute ‘sub_add’. It will, when later called as a method, receive the instance as the first parameter, bound to the local name ‘self’. > obj = cls() > self.sub[obj.id] = obj Here, ‘self’ will be an instance of the ‘A’ class. Each instance has no ‘sub’ attribute, so Python will find the class attribute ‘A.sub’, shared by all ‘A’ instances. You're then modifying that class attribute ‘A.sub’. […] > Now, what I get from this is the following: > <__main__.B instance at 0x01FC20A8> > <__main__.B instance at 0x01FC20A8> > Why is this? I hope the above explains it. > What I want is for them to be two separate objects, but it seems like > they are the same one. Yes. Anything you talk about in the class definition scope cannot know about any instance of that class, since the instances don't exist yet. Instead, instance attributes need to be bound to a particular instance, which means you need to have a reference to the instance; that's what ‘self’ is for. The class initialiser is a method named ‘__init__’, and is called on each newly-created instance before that instance is returned from the constructor. I advise you to work through the Python tutorial, beginning to end, which will give you a good grounding in these and other fundamental Python topics <URL:http://docs.python.org/tutorial/>. Work through each example, understand it by experimenting, and then proceed to the next, until you've done the lot. -- \ “If history and science have taught us anything, it is that | `\ passion and desire are not the same as truth.” —E. O. Wilson, | _o__) _Consilience_, 1998 | Ben Finney
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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