Python

I am shuffling my way through 'Python in easy steps'
I have reached the section on 'Classes' and cannot make the tutorials
work.
I have tried several variations on the attached code without success.
The first program Bird.py which sets up the class does not give any
errors but running instance.py always errors.
'Bird has no attribute _doc_' is an example of the first error.
Can anyone through any light/knowledge my way since I am getting
frustrated.
Code below.
Malcolm Smith
#!/C:\python3
#Bird.py
# Declare a new class with descriptive string
class Bird:
"""A base class to define bird properties."""
#Declare and initialise a class variable with integer value zero
count=0
#Define initialiser class method to initialise instance variable &
increment class variable
def _init_(self, chat):
self.sound = chat
Bird.count+= 1
# Add class method to return value of instance variable when called
#and save to file
def talk(self):
return self.sound
===================================================================
#!/C:\python3
#instance.py
#Make features of class file available then display its document
#string
from Bird import*
print('\nClass Instances Of:\n',Bird._doc_)
# statement to create an instance of the class & pass string value to
#its instance variable
polly = Bird('Squawk,squawk!')
#Display this instance variable value & call class method to display
#common class variable value
print('\nNumber Of Birds:',polly.count)
print('Polly Says:',polly.talk())
#Create second instance of class passing different string to instance
#variable
harry=Bird('Tweet, tweet!')
#Display instance variable value and call class method to display
#common class variable value
print('\nNumber Of Birds:',harry.count)
print('Harry Says:', harry.talk())
--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith
Loading thread data ...
I recall that you were a little frustrating yourself the last time you were here. You were advised multiple times by multiple people to make sure that you got the indentation correct, and a loooong time later you admitted that you didn't know what that meant. (I mean would it have killed you to Google it?)
*Sigh* anyway, glancing through that code, a few things stand out for me:
Linux on the raspberry Pi needs the unix-type "shebang" as the 1st line: #!/usr/bin/env python3
Are you using Windows? That Windows-like file path that you had in the shebang is a little odd since Windows doesn't use shebangs, but that brings me to the next point. Linux unlike Windows is case sensitive, so the file would have to be Bird.py not bird.py, although if you are using Windows, it might work anyway. But change it to be on the safe side.
Magic methods require double underscores on both the leading and trailing sides, which might not be obvious if the book uses a font that merges them together: __doc__ and __init__
And last but not least, the old indentation issue again. Everything from "def __init__" onwards needs an extra level of indentation.
Reply to
Dave Farrance
Sorry, ignore my comment regarding Bird.py since I see that you are using Bird.py consistently throughout. I'd jumped to the conclusion that it was a case sensitivity issue because linux usually uses lower case file names, and the error suggested that it simply couldn't find the file. In fact the error was almost certainly due to using single instead of double underscores in the magic methods.
Reply to
Dave Farrance
In message Dave Farrance wrote:
Yes I am using W7 then transferring the script to the Raspberry Pi
I am copying word for word from the book but not sure where the 'shebang' originated but have not effected previous scripts.
Very useful information that has solved the problem with the __doc__ statement. Despite all my googling and reading I have not found it stated anywhere that it was a double underscore. Everywhere it was shown as a single line.
If I indent any line in the script 'intance.py' I get the error 'unexpected indent' non are shown as indented in the book. If I run 'instance.py' as written in the book I get the error 'object() takes no parameters' and the polly=bird .... line is highlighted.
Thank you for your patience. I assure you I have google until I am lost in a sea of words
Malcolm
--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith
On Sun, 11 Jan 2015 22:47:24 GMT, T M Smith declaimed the following:
Then either the book is garbage, or the method used to read the book is faulty in how it renders indentation.
Python is highly dependent upon indentation -- where other language use
begin stuff end
(or {, }), Python uses the indentation level. Style guides for the other languages suggest visually delimiting blocks by indentation anyway; Python just does away with the begin/end ({/}) markers.
If you know pretty much any block structured language, reading the Python Language Reference and the Python Library Reference sections for data types should be enough to be able to produce functioning programs... they may not be elegant, but they will work.
--
	Wulfraed                 Dennis Lee Bieber         AF6VN 
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/
Reply to
Dennis Lee Bieber
instance.py has no structures requiring indentation, but Bird.py does.
Reply to
Dave Farrance
And since the indentation is so critical, if you get stuck again and need to ask the reason for an error, then as you did at the start of this thread, you'll have to paste in both files, exactly as they were when they generated the error.
Reply to
Dave Farrance
In case it hasn't already been mentioned, you can get odd results if you have a mixture of tab and space indents.
I have my text editor set so that the tab key sends spaces, not tab characters.
--
W J G
Reply to
Folderol
Thanks for your reply Folderol. Yes you picked out my inexcusable sin. A mixture of tabs and spaces.
Thanks for everyones input, I can now progress, a wiser person hope
Malcolm
--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith
I'm not a Python programmer so much of this passes me by, however, comments inline:

** If this is a declaration to force the use of a specific version/ location of python then it isn't portable between Windows and Linux. Familiar to me from Unix where #!/bin/sh says which shell to use for the following script **
** as above **
** Two comments wrapped together? **
** Noting that Pan has wrapped the code into the comment on reply, I'm wondering about your line terminations - probably just Pan, though **
** I don't understand why the declaration of the variable is commented out - this seems to make the print statement below useless **
--
Windows 8.1 on PCSpecialist box
Reply to
David
Thanks for the reply Dave. The code has lost something in translation. Your comment on the declaration may be valid but it does not seem to upset the running any of the files when transfered to the pi. So presumably it is ignored.
It turned out that the only problem with the code was I had used tabs instead of spaces in the first script, Bird.py.
Malcolm
--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.