Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #97974
| Subject | Re: None returned |
|---|---|
| Newsgroups | comp.lang.python |
| References | <b5e68b04-142f-4b78-89ee-874281914d5b@googlegroups.com> |
| From | Larry Hudson <orgnut@yahoo.com> |
| Date | 2015-10-28 00:23 -0700 |
| Message-ID | <meadnY2GAIVD6q3LnZ2dnUU7-dednZ2d@giganews.com> (permalink) |
On 10/27/2015 03:49 AM, pip7kids@gmail.com wrote:
> Hi
> In the example code below - why is "None" returned - print(rectangle.describe("this is a new rectangle")) - and how do I get to print out the result "this is a new rectangle"
>
> Regards
>
> class Shape:
>
> def __init__(self,x,y):
> self.x = x
> self.y = y
> self.description = "not yet described"
> self.author = "no text 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 author_name(self,text):
> self.author = text
>
> def scaleSize(self,scale):
> self.x = self.x * scale
> self.y = self.y * scale
>
> rectangle = Shape(100,45)
> print(rectangle.area())
> print(rectangle.perimeter())
> print(rectangle.describe("this is a new rectangle"))
> rectangle.scaleSize(0.5)
> print(rectangle.area())
>
You've already received answers about adding the return of the description text to your
describe() method. But I have a couple suggestions on how you might re-think your approach.
First, realize that setting the description text and reading (getting) it are two separate
operations. It might make better sense to write them that way -- as two separate methods.
Although that doesn't get you the ability to chain them as in your original (incorrect) example
because of the same 'None' problem, you still have to use them as two separate methods. In any
case, whichever way you choose to do this, you'll need to do it to your author_name() method as
well.
Second, your class name, Shape, implies this is a generic base class, but its definition is
specific to a Rectangle class. You should probably rewrite this in that manner -- as a base
class and a derived Rectangle class. (And a Circle class, Triangle class, and ...) I expect
that was your eventual intention anyway.
I would suggest that your Shape class should only implement the description and author
attributes, along with the methods to handle them. All the rectangle (or circle/triangle/...)
attributes and methods are in the derived classes. Something along the lines of this skeleton:
class Shape:
def __init__(self, desc='Shapeless'): # Allow specifying description when instantiated
self.description = desc # but still can use a default name
self.author = 'anonymous'
def get_desc(self):
return self.description
def set_desc(self, desc):
self.description = desc;
# Continue here with the author methods
...
class Rectangle(Shape):
def __init__(self, x=0, y=0): # I like using default values, YMMV
super().__init__('Rectangle') # self.description set to 'Rectangle'
# self.author set to default 'anonymous'
self.x, self.y = x, y # Personal taste, I like using this terse form
# Continue here with all the rectangle attributes/methods
...
Food for thought anyway. Keep at it.
-=- Larry -=-
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
None returned pip7kids@gmail.com - 2015-10-27 03:49 -0700 Re: None returned cassiobotaro@gmail.com - 2015-10-27 04:08 -0700 Re: None returned John Gordon <gordon@panix.com> - 2015-10-27 15:02 +0000 Re: None returned Larry Hudson <orgnut@yahoo.com> - 2015-10-28 00:23 -0700 Re: None returned Denis McMahon <denismfmcmahon@gmail.com> - 2015-10-28 08:33 +0000
csiph-web