Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
An Extra 'None'
#1
I'm trying a piece of script from Magnus Lie Hetland's book. It goes as below.

class Person:
    def setName(self, name):
        self.name = name

    def getName(self):
        return self.name

    def greet(self):
        print ("Hello world, I am %s." % self.name)

bar = Person()

bar.setName('Anakin Skywalker')

print (bar.greet())
When I run this script, besides the expected "Hello world, I am Anakin Skywalker", the "print (bar.greet())" gives me an extra line as "None".
Can someone tell me why there is a None?
Reply
#2
Your line 15 is like a print(print("some text")).
You just need to call bar.greet().
Reply
#3
(Oct-18-2018, 03:07 PM)leoahum Wrote: I'm trying a piece of script from Magnus Lie Hetland's book. It goes as below.

class Person:
    def setName(self, name):
        self.name = name

    def getName(self):
        return self.name

    def greet(self):
        print ("Hello world, I am %s." % self.name)

bar = Person()

bar.setName('Anakin Skywalker')

print (bar.greet())
When I run this script, besides the expected "Hello world, I am Anakin Skywalker", the "print (bar.greet())" gives me an extra line as "None".
Can someone tell me why there is a None?

Oh, thanks!
Reply
#4
(Oct-18-2018, 06:20 PM)leoahum Wrote:
(Oct-18-2018, 03:07 PM)leoahum Wrote: I'm trying a piece of script from Magnus Lie Hetland's book. It goes as below.

class Person:
    def setName(self, name):
        self.name = name

    def getName(self):
        return self.name

    def greet(self):
        print ("Hello world, I am %s." % self.name)

bar = Person()

bar.setName('Anakin Skywalker')

print (bar.greet())
When I run this script, besides the expected "Hello world, I am Anakin Skywalker", the "print (bar.greet())" gives me an extra line as "None".
Can someone tell me why there is a None?

Oh, thanks!

I liked the author's middle name Lie - very appropriate Snooty . This book - by your short examples - looks like a piece of garbage Naughty from a Java convert trying to ride a wave.

Neither names, not implementation of the setter/getter mechhaism (not that it is often used in Python) are Pythonic Angry.

To add insult to injury - old-style formatting Wall

The proper implementation may be something in the style below.

Output:
In [3]: class Person: ...: def __init__(self, name=''): ...: self._name = name ...: ...: @property ...: def name(self): ...: return self._name ...: ...: @name.setter ...: def name(self, name): ...: self._name = name ...: ...: def greet(self): ...: print ("Hello world, I am {}.".format(self.name)) ...: ...: p = Person('Nobody') ...: p.greet() ...: p.name = 'Anakin' ...: p.greet() ...: ...: Hello world, I am Nobody. Hello world, I am Anakin.
I would suggest for ind a book/learning resource by a legitimate Python specialist
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
(Oct-18-2018, 08:05 PM)volcano63 Wrote: I would suggest for ind a book/learning resource by a legitimate Python specialist

@volcano: Sometimes it's good to do some research before make statements like this
https://www.python.org/search/?q=Hetland
http://hetland.org/coding/

I guess it's old edition of Hetlands Begining Python book
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(Oct-18-2018, 08:14 PM)buran Wrote:
(Oct-18-2018, 08:05 PM)volcano63 Wrote: I would suggest for ind a book/learning resource by a legitimate Python specialist

@volcano: Sometimes it's good to do some research before make statements like this
https://www.python.org/search/?q=Hetland
http://hetland.org/coding/

I guess it's old edition of Hetlands Begining Python book

From https://www.python.org/dev/peps/pep-0008/
Quote:PEP: 8
Title: Style Guide for Python Code
Author: Guido van Rossum <guido at python.org>, Barry Warsaw <barry at python.org>, Nick Coghlan <ncoghlan at gmail.com>
Status: Active
Type: Process
Created: 05-Jul-2001
Post-History: 05-Jul-2001, 01-Aug-2013

The book I found by post-mortem Tongue research is dated by 2005.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020