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?
Your line 15
is like a print(print("some text"))
.
You just need to call bar.greet()
.
(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
. This book - by your short examples - looks like a piece of garbage
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
.
To add insult to injury - old-style formatting
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
(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
research is dated by 2005.