Jump to content

Problem with Python class encoding


Recommended Posts

Hi All

I have a little problem...

I have been brushing up on my python coding. I am the first to admitt it not up to scratch, so I have been working my way along a video series of tut, now I am half way along the intermidiate level and now into classes.

I have been trying the following code

class MyClass:
name = "noname"

def Main():
me = MyClass()
me.name = "mr ed"

print "name: " + me.name

if __name__ == '__Main__':
print "test"

Now in Windows using python 2.7.8 , it will print "test", but not 'print "name: " + me.name'

in linux it prints both, but comes up an error saying 'me' is not defined

can anyone point out where I am making a mistake.

Thank you

Link to comment
Share on other sites

Ah ..found it

the space in the line 'me.name = "mr ed" '

delete the space and it worked , can anyone explain why

Edited by Swamppifi
Link to comment
Share on other sites

If I had to guess, the problem likely had to do with leading whitespace being inconsistent. When programming in Python you should always use an editor that visualizes whitespace so that you can distinguish a tab from the equivalent width of spaces. Visually they may look the same, but to the Python interpreter they will look very different.

I generally prefer to use Vim. The following lines added to your .vimrc will help you out:

set listchars=tab:>-,trail:-
set list

On Windows, I will sometimes use Notepad++. Look for the button with the Paragraph icon. (It looks like a backwards P with two vertical lines.)

Link to comment
Share on other sites

Thanks for the tip sitwon

I am using notepad++ already, but thanks for the info on spacing

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...