list as an instance attribute

  • Thread starter Daniel Luis dos Santos
  • Start date
D

Daniel Luis dos Santos

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
 
A

André

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.

Cheers,

André
 
C

Chris Rebert

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



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
 
D

Daniel Santos

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 ?


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



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
 
R

r

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


You never initialized Primitive in CCylinder! And always write class
names with an initial uppercase letter...
class Primitive():
def...
 
B

Bruno Desthuilliers

Daniel Santos a écrit :
Here goes,

I have a base class that is the following :

class primitive:

pep08 : class names should be Capitalized.

Also, if you're using Python 2.x, make it:

class Primitive(object):
#...

def __init__(self):
self.name = ""
self.transforms = []

def copyInternalState(self, sourceObj, copyName):

pep08 : methods, attributes and variables names should be all_lower
return null
s/null/None/g



def copy(self, copyName):

# copy the source object internal state
primitiveCopy = self.copyInternalState(self, copyName)

# copy the transformations
primitiveCopy.transforms = []

If copyInternalState is supposed to return an instance of a
(well-behaved) subclass of 'primitive' - or any object conforming to the
(implicit) 'primitive' interface, you shouldn't have to do this.
if self.transforms != []:

An empty list has a false value in a boolean test, so the above test
would be better written as :

if self.transforms:

But anyway, the whole test is plain useless:

for item in []:
print "you won't see me"

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

You're not calling the __init__ method of primitive. Your code here
should be:

def __init__(self, name):
super(CircularCilinder, self).__init__(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)

Python is not Java. We have good support for computed properties, so you
*don't* need explicit getters/setters - just start with a plain
attribute access, you can always turn it into a computed one latter if
and when the need arises.

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'

Indeed - you didn't call the superclass's __init__
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.

Should be the same wrt/ the 'name' attribute FWIW.

HTH
 
R

Robin Becker

Bruno said:
Daniel Santos a écrit :

pep08 : class names should be Capitalized.

Also, if you're using Python 2.x, make it:

class Primitive(object):
#...
........

I find it remarkable that the most primitive classes appear to break the pep08
convention eg object, str, list etc etc. In fact all such conventions appear to
be broken more often than not. So the rule appears to be "create a convention
and then break it" :)

Python is often put forward as a as a finger friendly language, but we have
capitals encouraged for user class names and for some common values eg None,
True, False these are required.
 
M

Miles Kaufmann

.......

I find it remarkable that the most primitive classes appear to break
the pep08 convention eg object, str, list etc etc. In fact all such
conventions appear to be broken more often than not. So the rule
appears to be "create a convention and then break it" :)

More like "break a convention and then create it." :) Before Python
2.2, built-in types were not classes at all; they couldn't be
instantiated directly (from Python code), so you had to call the str()
function to create an object of type "string". I think there may have
been some discussion of renaming the built-ins to match PEP 8 for
Python 3, but if so I doubt it got very far.

-Miles
 
L

Lie Ryan

Robin said:
Python is often put forward as a as a finger friendly language, but we
have capitals encouraged for user class names and for some common values
eg None, True, False these are required.

And I'm glad it is, or else I'll get a finger-sore and an eye-sore
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top