From: Six on
I am trying to access an objects sub-object attributes. I can boil the
code I am working with down to this problem section:
(snip)
class Pt:
x = None
y = None
def __init__(self, x, y):
self.x, self.y = x, y
pass

class Pts:
curr_point = None
next_point = None
def __init__(self, n, m):
self.next_point = Pt(n, m)
def update(self, point):
self.curr_point = self.next_point
self.next_point = point

class PtManage:
points = {}
def __init__(self):
pass

point = Pts(3,5)
pman = PtManage()
pman.points["odds"] = point
print dir(pman)

print pman["odds"].next_point.x

(snip)

It's this last line that doesn't work. What am I doing wrong? Is this
a failure of the design or am I missing something obvious? How do I
get down and see that "Pt" classes x attribute within the PtManage
dict?
From: Sean DiZazzo on
On May 24, 9:34 pm, Six <john.d.perk...(a)gmail.com> wrote:
> I am trying to access an objects sub-object attributes. I can boil the
> code I am working with down to this problem section:
> (snip)
> class Pt:
>   x = None
>   y = None
>   def __init__(self, x, y):
>     self.x, self.y = x, y
>   pass
>
> class Pts:
>   curr_point = None
>   next_point = None
>   def __init__(self, n, m):
>     self.next_point = Pt(n, m)
>   def update(self, point):
>     self.curr_point = self.next_point
>     self.next_point = point
>
> class PtManage:
>   points = {}
>   def __init__(self):
>     pass
>
> point = Pts(3,5)
> pman = PtManage()
> pman.points["odds"] = point
> print dir(pman)
>
> print pman["odds"].next_point.x
>
> (snip)
>
> It's this last line that doesn't work. What am I doing wrong? Is this
> a failure of the design or am I missing something obvious? How do I
> get down and see that "Pt" classes x attribute within the PtManage
> dict?

Don't you mean?

pman.points["odds"].next_point.x
From: Benjamin Kaplan on
On Mon, May 24, 2010 at 9:34 PM, Six <john.d.perkins(a)gmail.com> wrote:
> I am trying to access an objects sub-object attributes. I can boil the
> code I am working with down to this problem section:
> (snip)
> class Pt:
>  x = None
>  y = None
>  def __init__(self, x, y):
>    self.x, self.y = x, y
>  pass
>
> class Pts:
>  curr_point = None
>  next_point = None

First of all, don't do this. Python doesn't have variable
declarations, only assignments. So this creates a variable called
curr_point for the *class*, not for the instance. What Java calls
static variables. It doesn't matter here but...

>  def __init__(self, n, m):
>    self.next_point = Pt(n, m)
>  def update(self, point):
>    self.curr_point = self.next_point
>    self.next_point = point
>
> class PtManage:
>  points = {}

Here you have a single mutable dict shared by all instances of PtManage.
a = PtManage()
b = PtManage()
a.points["a"] = Pts(3,2)
print b.points

>  def __init__(self):
>    pass
>
> point = Pts(3,5)
> pman = PtManage()
> pman.points["odds"] = point
> print dir(pman)
>
> print pman["odds"].next_point.x

PtManage doesn't define __getitem__, so pman["odds"] won't work.
pman.points["odds"] should.

>
> (snip)
>
> It's this last line that doesn't work. What am I doing wrong? Is this
> a failure of the design or am I missing something obvious? How do I
> get down and see that "Pt" classes x attribute within the PtManage
> dict?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
From: Terry Reedy on
On 5/25/2010 12:34 AM, Six wrote:
> [snip]
> It's this last line that doesn't work. What am I doing wrong?

When posting such questios, print the traceback if there is one or
otherwise describe 'does not work' in much more detail.


From: Chris Rebert on
On Mon, May 24, 2010 at 9:34 PM, Six <john.d.perkins(a)gmail.com> wrote:
> I am trying to access an objects sub-object attributes. I can boil the
> code I am working with down to this problem section:
> (snip)
> class Pt:
>  x = None
>  y = None
>  def __init__(self, x, y):
>    self.x, self.y = x, y
>  pass
>
> class Pts:
>  curr_point = None
>  next_point = None
>  def __init__(self, n, m):
>    self.next_point = Pt(n, m)
>  def update(self, point):
>    self.curr_point = self.next_point
>    self.next_point = point
>
> class PtManage:
>  points = {}
>  def __init__(self):
>    pass
>
> point = Pts(3,5)
> pman = PtManage()
> pman.points["odds"] = point
> print dir(pman)
>
> print pman["odds"].next_point.x
>
> (snip)
>
> It's this last line that doesn't work. What am I doing wrong? Is this
> a failure of the design or am I missing something obvious? How do I
> get down and see that "Pt" classes x attribute within the PtManage
> dict?

I suggest you read the part of Python's tutorial concerning classes
(http://docs.python.org/tutorial/classes.html ). Note that "curr_point
= None" and similar at the class level *does not* declare an object
field, because Python does not have instance variable declarations.
Here is a fixed and normalized version of the classes in your example:

class Pt(object):
def __init__(self, x, y):
self.x, self.y = x, y

class Pts(object):
def __init__(self, n, m):
self.curr_point = None
self.next_point = Pt(n, m)
def update(self, point):
self.curr_point = self.next_point
self.next_point = point

class PtManage(object):
def __init__(self):
self.points = {}


As for why your last line fails:
> print pman["odds"].next_point.x
As Sean said, you're missing a ".points":
print pman.points["odds"].next_point.x

Also, is there any reason for PtManage over just using a `points`
dictionary directly?

Cheers,
Chris
--
http://blog.rebertia.com