0

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:"

Ben
  • 23
  • 2
  • 5
  • Could you review your indentation; it's important in Python. – jonrsharpe Aug 17 '14 at 11:06
  • Indentation did not show up in my copy and paste. – Ben Aug 17 '14 at 11:17
  • 3
    Could not replicate - running the updated code (`pb = PlacesBook()`) works fine for me. Could you provide a [minimal example](http://stackoverflow.com/help/mcve) that allows others to recreate the issue? – jonrsharpe Aug 17 '14 at 11:32
  • This might answer your question: http://stackoverflow.com/questions/1802971/nameerror-name-self-is-not-defined – klasske Aug 17 '14 at 11:38

1 Answers1

-3

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
MoiTux
  • 798
  • 6
  • 17
  • The indentation did not show on the copy and paste. Also what do you mean by (object) – Ben Aug 17 '14 at 11:15
  • 6
    You have that backwards; in 3.x all classes are "new-style", you only need to explicitly specify `object` inheritance in 2.x. – jonrsharpe Aug 17 '14 at 11:20
  • I changed: class Stock(self, name, tankLocation, quantity, price, category, description, search): and now it says name'self' is not defined – Ben Aug 17 '14 at 11:20
  • @Ben the "arguments" to `class` are the base classes to inherit from (`object` in MoiTux's example, making this a ["new-style" class](https://docs.python.org/2/glossary.html#term-new-style-class)); `__init__` is the correct place to define the parameters to be passed on initialisation. – jonrsharpe Aug 17 '14 at 11:21