class initialization problem

R

rantingrick

ok i have a class and in it's constructor i want to create a copy of
it as an attribute, i have tried super, __new__, and noting seems to
work, please help!

class A(base):
def __init__(self):
super(A, self).__init__()
self.nested = ?

think of a nested list [ [] ] but with object "A" as the toplevel list
and having an instance of A in the attribute "nested"
 
A

alex23

ok i have a class and in it's constructor i want to create a copy of
it as an attribute, i have tried super, __new__, and noting seems to
work, please help!

class A(base):
    def __init__(self):
        super(A, self).__init__()
        self.nested = ?

think of a nested list [ [] ] but with object "A" as the toplevel list
and having an instance of A in the attribute "nested"

Sorry, do you want an a copy of the instance or of the class?

If you mean the class, self.nested = self.__class__
If you mean the instance, self.nested = self
 
R

rantingrick

ok i have a class and in it's constructor i want to create a copy of
it as an attribute, i have tried super, __new__, and noting seems to
work, please help!
class A(base):
    def __init__(self):
        super(A, self).__init__()
        self.nested = ?
think of a nested list [ [] ] but with object "A" as the toplevel list
and having an instance of A in the attribute "nested"

Sorry, do you want an a copy of the instance or of the class?

If you mean the class, self.nested = self.__class__
If you mean the instance, self.nested = self

No i want an *actual* separate instance inside the current instance
bound to an attribute "nested". But the problem is how to do this in
the constructor without causing infinite recursion. I thought about
creating an object copy function which would initialize the object and
*then* return it to the constructor but that seemed kind of kludgy.
There must be some syntax for doing this?
 
C

Carl Banks

ok i have a class and in it's constructor i want to create a copy of
it as an attribute, i have tried super, __new__, and noting seems to
work, please help!

class A(base):
    def __init__(self):
        super(A, self).__init__()
        self.nested = ?

think of a nested list [ [] ] but with object "A" as the toplevel list
and having an instance of A in the attribute "nested"

Well, to answer the question you asked ("i have a class and in it's
constructor i want to create a copy of it as an attribute"):

import copy

self.nested = copy.copy(self)


However, your post contains some conflicting information, "copy" often
means different things to different people, "it" is ambiguous, and
what you ask for seems to to be well-conceived. I think we will be
able to help you more if you give more details about what you expect
and how you intend to use this nested object.

Please try to observe the distiction between classes and instances
(you almost certainly wanted a copy of the instance, not of the
class).


Carl Banks
 
R

rantingrick

ok i have a class and in it's constructor i want to create a copy of
it as an attribute, i have tried super, __new__, and noting seems to
work, please help!
class A(base):
    def __init__(self):
        super(A, self).__init__()
        self.nested = ?
think of a nested list [ [] ] but with object "A" as the toplevel list
and having an instance of A in the attribute "nested"

Well, to answer the question you asked ("i have a class and in it's
constructor i want to create a copy of it as an attribute"):

import copy

self.nested = copy.copy(self)

However, your post contains some conflicting information, "copy" often
means different things to different people, "it" is ambiguous, and
what you ask for seems to to be well-conceived.  I think we will be
able to help you more if you give more details about what you expect
and how you intend to use this nested object.

Please try to observe the distiction between classes and instances
(you almost certainly wanted a copy of the instance, not of the
class).

Carl Banks

ok here is some code. this will cause an infinite recursion.

class A():
def __init__(self, *args):
self.nestedA = A(*args) #NO GOOD!

there must be a way to create an instance of an object within the same
objects constructor?

TIA
 
A

alex23

ok here is some code. this will cause an infinite recursion.

class A():
    def __init__(self, *args):
        self.nestedA = A(*args) #NO GOOD!

there must be a way to create an instance of an object within the same
objects constructor?

But if _every_ instance of A has a 'nested' instance of A, won't it
_always_ end up smacking up against the recursive depth limit?

It's beginning to sound like you need to separate the creation out to
a factory function. And to rethink your design, maybe.
 
O

OKB (not okblacke)

rantingrick said:
ok here is some code. this will cause an infinite recursion.

class A():
def __init__(self, *args):
self.nestedA = A(*args) #NO GOOD!

there must be a way to create an instance of an object within the
same objects constructor?

This is an inherent problem with what you want to do, not a problem
with Python. You have said you want each instance of A to have a nested
instance. But then the nested instance is an instance of A too, so it
should have a nested instance, and that doubly-nested instance should
have its own nested instance, and so on to infinity.

Perhaps you want to cut off the recursion at the first step, so
that the nested instance itself does not have a nested instance. If so,
add another parameter to __init__ that flags whether you are creating a
"top-level" instance.

--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
 
R

rantingrick

!SOLVED!
Thanks for the help guys!

copy.copy did it!

Why does me makes life so hard on me? ;-)
 
R

rantingrick

        Perhaps you want to cut off the recursion at the first step, so
that the nested instance itself does not have a nested instance.  If so,
add another parameter to __init__ that flags whether you are creating a
"top-level" instance.


yes, i also figured that out just a few minutes ago.

class A():
def __init__(self, nested=True)
if nested:
self.var = A(nested=False)

I think i have been staring at code too long and my noodle just
shutdown for the evening :)

Thanks!
 
A

alex23

ok here is some code. this will cause an infinite recursion.

class A():
    def __init__(self, *args):
        self.nestedA = A(*args) #NO GOOD!


How about:

class A(object):
def __init__(self, first=True, *args):
if first:
self.nestedA = A(first=False, *args)

That's about the only way I can see you easily avoiding the
recursiveness.
 
A

alex23

How about:

  class A(object):
      def __init__(self, first=True, *args):
      if first:
          self.nestedA = A(first=False, *args)

That's about the only way I can see you easily avoiding the
recursiveness.

Only, y'know, with the right indentation:

class A(object):
def __init__(self, first=True, *args):
if first:
self.nestedA = A(first=False, *args)
 
C

Carl Banks

ok i have a class and in it's constructor i want to create a copy of
it as an attribute, i have tried super, __new__, and noting seems to
work, please help!
class A(base):
    def __init__(self):
        super(A, self).__init__()
        self.nested = ?
think of a nested list [ [] ] but with object "A" as the toplevel list
and having an instance of A in the attribute "nested"
Well, to answer the question you asked ("i have a class and in it's
constructor i want to create a copy of it as an attribute"):
import copy
self.nested = copy.copy(self)
However, your post contains some conflicting information, "copy" often
means different things to different people, "it" is ambiguous, and
what you ask for seems to to be well-conceived.  I think we will be
able to help you more if you give more details about what you expect
and how you intend to use this nested object.
Please try to observe the distiction between classes and instances
(you almost certainly wanted a copy of the instance, not of the
class).
Carl Banks

ok here is some code. this will cause an infinite recursion.

class A():
    def __init__(self, *args):
        self.nestedA = A(*args) #NO GOOD!

there must be a way to create an instance of an object within the same
objects constructor?


Here's something to ponder:

If you have two objects, one of which has an attribute that contains
certain nested object, one which doesn't, should those objects be
instances of the same class?


You may come to a different conclusion, but it seems to me they ought
to be different classes. They have a significant difference in their
behavior.

class Inner(base):
def __init__(self,*args):
super(self,Inner).__init__(*args)

class Outer(Inner):
def __init__(self,*args):
super(self,Outer).__init__(*args)
self.nested = Inner(*args)


If you insist on them being the same class, you could use copy.copy()
as I did earlier, although it won't always work for every class. Or
just pass a parameter to the class indicating whether it should create
a nested copy of itself, defaulting to True. When creating the nested
class it should be set to False. (Note: because keyword-only
arguments aren't in Python 2.5 I capture _create_nested in kwargs.
You may find it more convenient to use a keyword argument.)

class A(base):
def __init__(self,*args,**kwargs):
_create_nested = kwargs.pop('_create_nested',True)
super(A,self).__init__(*args,**kwargs)
if _create_nested:
self.nested = A(_create_nested=False,*args)


Carl Banks
 
R

rantingrick

EDIT:

copy.copy did work in a simple python interactive session, but it
caused infinite recursion in my real world code? Anyway what ever is
going on (probably lack of sleep!) the cure all was using an arg in
the initial function. So now i am good as gold ;-)

WHY did i need do this you may ask?
I am creating geometry with OpenGL. When you create a face you must
specify a winding order (clockwise or counter clockwise) this winding
order controls which side of the face will be visible (since only one
side of a face is rendered). So to create a two sided face you must
create two faces that share the exact same points, BUT have opposite
windings, hence the need to create a face with a "backface" attribute
that contains the mirrored face instance. So now i can move the
frontface around and i have a handy reference to the backface so i can
make it follow!


class Face:
def __init__(self, pts, nested=True):
if nested:
newpts = reverse(pts)
self.backface = Face(pts, nested=False)
def translate(self, v):
#offset front and back face by vector

two sided faces, yippie!

And so ends another long day of coding. This is addicting. Does
anybody know of a good Coders Anonymous group, i think i may have an
addiction. 16 hours of strait coding with 4hrs sleep last night and
only stopping for one meal (lunch) still have not ate dinner yet!
Gheez! ;)
 
G

Gabriel Genellina

I am creating geometry with OpenGL. When you create a face you must
specify a winding order (clockwise or counter clockwise) this winding
order controls which side of the face will be visible (since only one
side of a face is rendered). So to create a two sided face you must
create two faces that share the exact same points, BUT have opposite
windings, hence the need to create a face with a "backface" attribute
that contains the mirrored face instance. So now i can move the
frontface around and i have a handy reference to the backface so i can
make it follow!


class Face:
def __init__(self, pts, nested=True):
if nested:
newpts = reverse(pts)
self.backface = Face(pts, nested=False)
def translate(self, v):
#offset front and back face by vector

two sided faces, yippie!

I'd use a factory:

class Face:
otherside = None
def __init__(self, pts):
self.pts = pts

class BiFace(Face):
otherside = None

def BiFaceFactory(pts):
front = BiFace(pts)
back = BiFace(list(reversed(pts)))
front.otherside = back
back.otherside = front
return front, back
And so ends another long day of coding. This is addicting. Does
anybody know of a good Coders Anonymous group, i think i may have an
addiction. 16 hours of strait coding with 4hrs sleep last night and
only stopping for one meal (lunch) still have not ate dinner yet!
Gheez! ;)

Remember, there is a whole world out there!
 
D

Dave Angel

rantingrick said:
yes, i also figured that out just a few minutes ago.

class A():
def __init__(self, nested=ue)
if nested:
self.var =(nestedúlse)

I think i have been staring at code too long and my noodle just
shutdown for the evening :)

Thanks!
Don't forget the else: clause. If the nested flag is not set, set
self.var = None. Otherwise, some user of this class has to face the
possibility that he gets an attribute exception accessing the child.

DaveA
 
D

Dennis Lee Bieber

No i want an *actual* separate instance inside the current instance
bound to an attribute "nested". But the problem is how to do this in
the constructor without causing infinite recursion. I thought about
creating an object copy function which would initialize the object and
*then* return it to the constructor but that seemed kind of kludgy.
There must be some syntax for doing this?

So add a flag to stop the recursion...

class A(base):
def __init__(self, recurse=True):
super(A, self).__init__()
if recurse:
self.nested = A(recurse=False)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top