Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #97967 > unrolled thread
| Started by | pip7kids@gmail.com |
|---|---|
| First post | 2015-10-27 03:49 -0700 |
| Last post | 2015-10-28 08:33 +0000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | pip7kids@gmail.com |
|---|---|
| Date | 2015-10-27 03:49 -0700 |
| Subject | None returned |
| Message-ID | <b5e68b04-142f-4b78-89ee-874281914d5b@googlegroups.com> |
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())
[toc] | [next] | [standalone]
| From | cassiobotaro@gmail.com |
|---|---|
| Date | 2015-10-27 04:08 -0700 |
| Message-ID | <c9c5b0b5-f677-43d4-b0da-293d7238460a@googlegroups.com> |
| In reply to | #97967 |
it's because you only set a value in a instance. If you want to return the inserted text do:
def describe(self, text):
self.description = text
return text
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2015-10-27 15:02 +0000 |
| Message-ID | <n0o3n3$s3n$1@reader1.panix.com> |
| In reply to | #97967 |
In <b5e68b04-142f-4b78-89ee-874281914d5b@googlegroups.com> pip7kids@gmail.com writes:
> print(rectangle.describe("this is a new rectangle"))
You're printing the result of describe().
> def describe(self,text):
> self.description = text
describe() has no return value, so it returns None by default.
If you want it to return something, make it do that.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Larry Hudson <orgnut@yahoo.com> |
|---|---|
| Date | 2015-10-28 00:23 -0700 |
| Message-ID | <meadnY2GAIVD6q3LnZ2dnUU7-dednZ2d@giganews.com> |
| In reply to | #97967 |
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 -=-
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-10-28 08:33 +0000 |
| Message-ID | <n0q17v$gup$3@dont-email.me> |
| In reply to | #97967 |
On Tue, 27 Oct 2015 03:49:29 -0700, pip7kids wrote:
> Hi In the example code below - why is "None" returned -
A function with no explicit return statement returns the non value None.
Your shape.describe function sets an internal description, but has no
return value.
Note that your author_name member function has the same issue as the
describe function.
It might also be useful to distinguish between setter and getter
functions:
def set_describe(self,text):
self.description = text
def set_author_name(self,text):
self.author = text
def get_describe(self):
return self.description
def get_author_name(self):
return self.author
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web