list as an instance attribute
Daniel Santos
daniel.dlds at gmail.com
Sun Sep 13 20:34:29 EDT 2009
Here goes,
I have a base class that is the following :
class primitive:
def __init__(self):
self.name = ""
self.transforms = []
def copyInternalState(self, sourceObj, copyName):
return null
def copy(self, copyName):
# copy the source object internal state
primitiveCopy = self.copyInternalState(self, copyName)
# copy the transformations
primitiveCopy.transforms = []
if self.transforms != []:
for transf in self.transforms:
primitiveCopy.transforms.append(transf.copy())
return primitiveCopy
# more methods. the ones listed should be enough to get the picture
And a derived class,
class CircularCilinder(primitive):
def __init__(self, name):
self.name = name
self.baseCenterVertex = [0, 0, 0]
self.heightVertex = [0, 1, 0]
self.radius = 1
def copyInternalState(self, sourceObj, copyName):
copy = CircularCilinder(copyName)
copy.setHeight(self.heightVertex[1])
copy.setRadius(self.radius)
return copy
I then have a script that creates a CircularCilinder,
cilinder = CircularCilinder("cilinder")
cilinderCopy = cilinder.copy("cilinderCopy")
when I run this script I get the error,
Traceback (most recent call last):
File "./testscript.py", line 26, in <module>
cilinderCopy = cilinder.copy("cilinderCopy")
File "/cygdrive/d/cg/brlcad_api/trunk/src/ds_brlcad_modeler/api/primitive.py",
line 64, in copy
if self.transforms != []:
AttributeError: CircularCilinder instance has no attribute 'transforms'
the transforms instance attribute is not known in the derived class. But I
wanted to avoid adding a self.transforms = [] line to the derived class
__init__ method. That attribute is only to be known by the superclass.
Is there a way to do this ?
On Sat, 12 Sep 2009 20:28:12 -0700, Chris Rebert wrote:
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>
>
>
> On Sat, Sep 12, 2009 at 8:22 PM, André <andre.roberge at gmail.com> wrote:
>> On Sep 12, 11:48 pm, Daniel Luis dos Santos <daniel.d... at gmail.com>
>> wrote:
>>> Hello,
>>>
>>> I have an object definition :
>>>
>>> class primitive:
>>>
>>> def __init__(self)
>>> self.name = ""
>>> self.transforms = []
>>>
>>> def copy(self, copyName)
>>>
>>> copy = self.copyInternalState(copyName) # method defined elsewhere
>>> in derived class
>>>
>>> if self.transforms != []
>>> for transf in self.transforms
>>> copy.transforms.append(transf.copy())
>>>
>>> In short, what I want to is to have the transforms list as an instance
>>> attribute of the class. I will add objects to it. When I call the copy
>>> method on the object the list is to be copied to the new object.
>>>
>>> Problem is that the python interpreter is complaining that it doesn't
>>> know any self.transforms in the if statement.
>>>
>>> Help
>>
>> 1. Always post the actual code - the code you posted is not valid
>> Python as many colons (:) are missing.
>> 2. You appear to be attempting to use the same name (copy) both as a
>> method name AND as a local variable of that method. This definitely
>> look wrong.
>>
>> Perhaps if you could post the actual offending code (the smallest
>> example showing the problem you observe) others might be able to help
>> you.
>
> The full, exact error message and exception traceback would also be helpful.
>
> Cheers,
> Chris
More information about the Python-list
mailing list