简体   繁体   中英

builtins.NameError: name 'self' is not defined

Using Python 3 on Windows 7.

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

I get the error builtins.NameError: name 'self' is not defined at "class PlacesBook:"

The problem is with your indentation, in Python the indention is very important, that's how you define what part of code is in the class, method, ...

An other point if you do python 3 all your classes must inherit from 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM