0
class Shape:

    def __init__(self, x, y, name, age):
        self.x = x
        self.y = y
        self.name = name
        self.age = age
        description = "This shape has not been described yet"
        author = "Nobody has claimed to make this shape yet"

    def area(self):
        return self.x * self.y

    def perimeter(self):
        return 2 * self.x + 2 * self.y

    def describe(self,text):
        self.description = text

    def authorName(self,text):
        self.author = text

    def scaleSize(self,scale):
        self.x = self.x * scale
        self.y = self.y * scale

How do i print the attributes in authorName , describe and scaleSize as so far i have only been able achieve this result.

objectname = Shape(8,10, 'Peter Adom', '55')

print(objectname.name)
print(objectname.area())
print(objectname.perimeter())
2
  • describe is not a "getter" it is a "setter". It acts to set the author name for some Shape object. You would need some function def printAuthor(self): print(self.author) then you could call objectname.printAuthor() Commented Aug 16, 2014 at 19:44
  • 2
    you can print it directly ? print objectname.author . Commented Aug 16, 2014 at 19:51

1 Answer 1

1

You can see that the functions area and perimeter have a return clause, that's because they are supposed to compute something that would not be directly accessible (you have x and y but you don't have an attribute that stores the area or the perimeter. This is common in programming because if you had an area attribute, this could differ from the value computed by self.x * self.y, bringing inconsistencies.

The methods describe, authorName and scaleSize instead are used to change some of the attributes of the object. They do not return anything, they set values in the object.

In order to retrieve the attributes of the object you should just access them directly. But this doesn't work for description and author because you are not setting the attributes in the object namespace. In fact the lines in method __init__ should be:

self.description = "This shape has not been described yet"
self.author = "Nobody has claimed to make this shape yet"

After that you will be able to do:

objectname = Shape(8,10, 'Peter Adom', '55')

print(objectname.author)             # Nobody has claimed to make this shape yet
objectname.authorName("Me")
print(objectname.author)             # Me

print(objectname.description)             # This shape has not been described yet
objectname.describe("Wonderful shape")
print(objectname.description)                  # Wonderful shape

You should consider naming your setter in a way that makes unequivocal their purpose, for example setAuthor and setDescription.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.