Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #47712
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-06-11 16:50 -0700 |
| References | <kp4jf4$5fu$1@dont-email.me> |
| Message-ID | <1f11f483-e152-4fe3-8b11-e83bacf2ed63@googlegroups.com> (permalink) |
| Subject | Re: Newbie: question regarding references and class relationships |
| From | Rick Johnson <rantingrickjohnson@gmail.com> |
On Monday, June 10, 2013 8:18:52 AM UTC-5, Rui Maciel wrote:
> [...]
>
> <code>
> class Point:
> position = []
> def __init__(self, x, y, z = 0):
> self.position = [x, y, z]
Firstly. Why would you define a Point object that holds it's x,y,z values in a list attribute? Why not store them as self.x, self.y and self.z? But if you are going to store them as a list, then why not extend a python list? I hate to see a "listy" object that only holds a list. It begs the question: Why not make it a *real* list? (Although i believe the former approach is more desirable for this problem)
Secondly, why would store the position of the Point as a class attribute? Do you realize that EVERY instance of the Point object will share the same x,y, and z values if you do it that way? (considering you query the correct variable *evil grin*)
Actually, you have a legitimate excuse: you were fooled because in your "object definition" you declared a "class level variable" named "position". THEN in your "__init__" method you declared an "instance level variable" named "position".
Then, when you assigned the x,y,z values to "self.position" you thought you where assigning them to the "class level variable" named "position", HaHa, but then again you had NO idea that "class level variables" and "instance level variables" even existed! (or, at least, how to properly declare them)
Q: Yes Rick but how do i solve this issue?
By changing the names we can inspect the Point object and understand how class level and instance level variables work in Python. Observe:
## START SESSION ##
py> class Foo(object):
... classVar = []
... def __init__(self, arg):
... self.instanceVar = arg
...
py> Foo.classVar
[]
py> Foo.classVar = "apple"
py> Foo.classVar
apple
py> foo1 = Foo("pear")
py> foo1.classVar
apple
py> foo1.instanceVar
pear
py> foo2 = Foo("peach")
py> foo2.classVar
apple
py> foo2.instanceVar
peach
## END SESSION ##
As you can see the "class level variable" is known to all instances of the object, and a change in one is equal to a change in all. Whereas the "instance level variables" are unique to each instance.
> class Line:
> points = ()
> def __init__(self, p_i, p_f):
> self.points = (p_i, p_f)
Same problem here with the class level/instance level thing.
> It would be nice if, whenever a Point object was updated,
> the Line objects which are associated with it could
> reflect those updates directly in Line.p_i and Line.p_f.
> What's the Python way of achieving the same effect? Thanks
> in advance, Rui Maciel
>
If you construct your code properly this can be achieved. If each point is an object, and lines are merely holding references to two point objects that define the start and end position of an imaginary "line", then updates on the points will be reflected in the Line object.
Observe:
## START SESSION ##
py> class Point3d(object):
... def __init__(self, x, y, z):
... self.x = x
... self.y = y
... self.z = z
... def __str__(self):
... _ = 'Point3d({}, {}, {})'
... return _.format(self.x, self.y, self.z)
...
py> p1 = Point3d(1,2,3)
py> str(p1)
Point3d(1, 2, 3)
py> p2 = Point3d(3,4,5)
py> str(p2)
Point3d(3, 4, 5)
py> class Line3d(object):
... def __init__(self, p1, p2):
... self.p1 = p1
... self.p2 = p2
... def __str__(self):
... _ = 'Line3d({}, {})'
... return _.format(self.p1, self.p2)
...
py> line1 = Line3d(p1, p2)
py> str(line1)
Line3d(Point3d(1, 2, 3), Point3d(3, 4, 5))
py> p1.x = 100
py> str(p1)
Point3d(100, 2, 3)
py> str(line1)
Line3d(Point3d(100, 2, 3), Point3d(3, 4, 5))
## END SESSION ##
Easy peasy.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 14:18 +0100
Re: Newbie: question regarding references and class relationships Roy Smith <roy@panix.com> - 2013-06-10 09:35 -0400
Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 14:57 +0100
Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 15:00 +0100
Re: Newbie: question regarding references and class relationships Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-10 13:56 -0600
Re: Newbie: question regarding references and class relationships Rick Johnson <rantingrickjohnson@gmail.com> - 2013-06-11 17:02 -0700
Re: Newbie: question regarding references and class relationships Peter Otten <__peter__@web.de> - 2013-06-10 15:49 +0200
Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 14:59 +0100
Re: Newbie: question regarding references and class relationships Peter Otten <__peter__@web.de> - 2013-06-10 16:28 +0200
Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 15:38 +0100
Re: Newbie: question regarding references and class relationships Peter Otten <__peter__@web.de> - 2013-06-10 17:05 +0200
Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 16:20 +0100
Re: Newbie: question regarding references and class relationships Peter Otten <__peter__@web.de> - 2013-06-10 17:55 +0200
Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 17:09 +0100
Re: Newbie: question regarding references and class relationships Peter Otten <__peter__@web.de> - 2013-06-10 19:07 +0200
Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 18:42 +0100
Re: Newbie: question regarding references and class relationships Dave Angel <davea@davea.name> - 2013-06-10 15:36 -0400
Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 21:31 +0100
Re: Newbie: question regarding references and class relationships Terry Jan Reedy <tjreedy@udel.edu> - 2013-06-10 15:51 -0400
Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 21:13 +0100
Re: Newbie: question regarding references and class relationships Terry Jan Reedy <tjreedy@udel.edu> - 2013-06-10 18:07 -0400
Re: Newbie: question regarding references and class relationships Grant Edwards <invalid@invalid.invalid> - 2013-06-10 22:39 +0000
Re: Newbie: question regarding references and class relationships Chris Angelico <rosuav@gmail.com> - 2013-06-11 08:54 +1000
Re: Newbie: question regarding references and class relationships Tim Chase <python.list@tim.thechases.com> - 2013-06-10 18:24 -0500
Re: Newbie: question regarding references and class relationships Dave Angel <davea@davea.name> - 2013-06-10 19:26 -0400
Re: Newbie: question regarding references and class relationships Jason Swails <jason.swails@gmail.com> - 2013-06-10 23:49 -0400
Re: Newbie: question regarding references and class relationships Terry Jan Reedy <tjreedy@udel.edu> - 2013-06-10 15:54 -0400
Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 21:09 +0100
Re: Newbie: question regarding references and class relationships Rick Johnson <rantingrickjohnson@gmail.com> - 2013-06-11 16:50 -0700
Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-13 13:06 +0100
Re: Newbie: question regarding references and class relationships Chris Angelico <rosuav@gmail.com> - 2013-06-14 00:07 +1000
Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-13 19:23 +0100
csiph-web