[Tutor] problem with program in python in easy steps
Alan Gauld
alan.gauld at yahoo.co.uk
Thu Oct 26 20:04:45 EDT 2017
On 26/10/17 21:02, Chris Coleman wrote:
> i wrote these programs and saved them per instructions on page 128 and 129
> in the book "python in easy steps".
I don't know the book but...
>
> class Person:
> '''A base class to define Person properties.'''
> def__init__(self,name):
> self.name = name
> def speak( self,msg = '(Calling The Base Class)'):
> print(self.name,msg)
>
I assume the book suggests storing these class definitions
in separate files named after the classes? (A small point
is that usual style is to name modules in all lower case)
> from Person import*
> '''A derived class to define Man properties.'''
The string should start at the beginning of the line - it
is not inside a new block so should not be indented.
I assume the book recommends the from x import * style but
thats considered bad practice. Only import the names you
need or, if there are many, import just the module name
(possibly with an alias). Like so:
from modulename import name1, name2,etc...
import modulename
import modulename as alias
The import * style can lead to weird bugs due to
name collisions.
> class Man(Person):
> def speak(self,msg):
> print(self.name,':\n\tHello!',msg)
>
> from Person import*
> '''A derived class to define Hombre properties.'''
Same problem with the string definition.
> class Hombre(Person):
> def speak(self,msg):
> print(self.name,':\n\tHola!',msg)
>
> from Man import*
> from Hombre import*
> guy_1 = Man('Richard')
> guy_2 = Hombre('Ricardo')
> guy_1.speak('It\'s a beautiful evening.\n')
> guy_2.speak('Es una tarde hermosa.\n')
> Person.speak(guy_1)
> Person.speak(guy_2)
>
> i ran the program override.py and get this error message:
>
> Traceback (most recent call last):
> File "scripts/override.py", line 1, in <module>
> from Man import*
> File "/home/chris/scripts/Man.py", line 2
> '''A derived class to define Man properties.'''
> ^
> IndentationError: unexpected indent
And this is telling you not to indent your string.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list