开发者

Python Interpreter Not Reading Indents Properly After Turning on VIM Auto-Indenting

开发者 https://www.devze.com 2023-04-07 01:32 出处:网络
I\'m not entirely sure what has happened. I was debugging a script that worked \"fine\" (except for said intermittent bugs) and all of a sudden the module cannot be imported anymore. I undid all cha

I'm not entirely sure what has happened.

I was debugging a script that worked "fine" (except for said intermittent bugs) and all of a sudden the module cannot be imported anymore. I undid all changes and still the problem exists. Well, problemS. Plural.

Once I got an "unexpected indent" error, even though all the lines were perfectly indented. I fixed that one by deleting the line and retyping it.

Now in the following code, I get one of two errors:

class Lottery:
    def __init__(self, session):
          self.prizes = PrizeList()
          self.session = session
          self.players = self.session.listof.players.split(',')
          self.pickWinner()

Most of the time, it gives me an error saying "session" is not defined. This is true. I'm just importing the module. It gets defined when the script calling it runs it. I experimented with just removing tha开发者_Go百科t line altogether, and then it told me that "self" was not defined.

All of that is the original code that was working 20 minutes ago. the bugs I was fixing are on a different part of this module altogether, and it was certainly importing without problems. Please help!

Traceback:

  File "minecraft/mcAdmin.py", line 5, in <module>
    from lottery.lottery import *
  File "/home/tomthorogood/minecraft/lottery/lottery.py", line 36, in <module>
    class Lottery:
  File "/home/tomthorogood/minecraft/lottery/lottery.py", line 39, in Lottery
    self.session = session
NameError: name 'session' is not defined

EDIT: SOLVED. Okay, somehow while editing, I did accidentally switch between tabs and spacing, which lead to the problem. I deleted and re-wrote that block of code exactly as pasted above and now it's working. There was a ghost indent problem.

DOUBLE EDIT: the core problem was that I only recently turned on auto-indenting in Vim. The configuration I am using is not using tabs as auto-indents, but I was using tabs in the past.


You've got an indentation problem.

The self.session = session line, and everything below, isn't inside your __init__ method, it's just inside the class body.

session isn't defined in the class body, only inside __init__, as you mention in your question, so you get the error.

IF you remove that line, the first thing that gets looked up is the self in self.players = self.session.listof.players.split(','), so you get the self is not defined error.

0

精彩评论

暂无评论...
验证码 换一张
取 消