builtins.NameError:未定义名称“self”

时间:2014-08-17 10:58:22

标签: python python-3.x

在Windows 7上使用Python 3。

import pickle
import os.path
from tkinter import * # Import tkinter
import tkinter.messagebox   

class Places:
  def __init__(self, name, street, city, state, zip):
    self.name = name
    self.street = street
    self.city = city
    self.state = state
    self.zip = zip

class PlacesBook:
  def __init__(self):      
    window = Tk() # Create a window
    window.title("PlacesBook") # Set title

我收到错误builtins.NameError:name' self'未定义在"类PlacesBook:"

1 个答案:

答案 0 :(得分:-3)

问题出在你的缩进上,在Python中,缩进是非常重要的,这就是你如何定义代码的哪一部分在类,方法中......

如果你做python 3,另一点你所有的类都必须从object继承。

import pickle
import os.path
from tkinter import * # Import tkinter
import tkinter.messagebox

class Places(object):
    def __init__(self, name, street, city, state, zip):
        self.name = name
        self.street = street
        self.city = city
        self.state = state
        self.zip = zip

class PlacesBook(object):
    def __init__(self):
        window = Tk() # Create a window
        window.title("PlacesBook") # Set title