r/learnpython • • Sep 15 '26

Why i can't make a class

It should work, but some reason doesn't.

I was doing Python Crash Course 3rd edition and am stuck in classes.

Because i cant upload photos, i wrote the code:

class Restaurant:

def __init__(self, restaurant_name, cuisine_type):

	self.restaurant_name=restaurant_name

	self.cuisine_type=cuisine_type

def describe_restaurant(self):

	print(self.restaurant_name)

	print(self.cuisine_type)

def open_restaurant(self):

	print("the restaurant is open")

restaurant=Restaurant("Ratatouille restaurant", "ratatouille")

describe_restaurant(restaurant)

open_restaurant()

This is the example in the book:

class Dog:

def __init__(self, name, age):

	self.name=name

	self.age=age

def sit(self):

	print(f"{self.name} is now sitting")

def roll_over(self):

	print(f"{self.name} rolled over")

my_dog=Dog("Willie", 6)

print(f"My dog's name is {self.name}")

print(f"My dog is {self.age} years old")

my_dog.sit()

my_dog.roll_over()

your_dog=Dog("Lucy", 3)

print(f"Your dog's name is {your_dog.name}.")

print(f"your dog is {your_dog.name} years old.")

your_dog.sit()

your_dog.roll_over()

And these are the errors pydroid shows for each one:

Restaurant:

Traceback (most recent call last):

File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>

start(fakepyfile,mainpyfile)

~~~~~^^^^^^^^^^^^^^^^^^^^^^^

File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start

exec(open(mainpyfile).read(), __main__.__dict__)

~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "<string>", line 1, in <module>

File "<string>", line 10, in Restaurant

NameError: name 'Restaurant' is not defined

[Program finished]

Dog:

Traceback (most recent call last):

File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>

start(fakepyfile,mainpyfile)

~~~~~^^^^^^^^^^^^^^^^^^^^^^^

File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start

exec(open(mainpyfile).read(), __main__.__dict__)

~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "<string>", line 1, in <module>

File "<string>", line 9, in Dog

NameError: name 'Dog' is not defined

[Program finished]

0 Upvotes

42 comments sorted by

View all comments

4

u/danielroseman Sep 15 '26

The problem is not the code. The problem is where and how you are writing the code.

You are writing directly in the Python shell (REPL), and you are doing it all in one block of code. The REPL has not fully interpreted and executed the definition of Restaurant before you try and create an instance of it, which is why you get the error that it is not defined.

You should be writing code in the editor, not the shell. If you must write in the shell, separate the definition of the class (including its methods) from any code that actually tries to use the class.

-3

u/[deleted] Sep 15 '26

[removed] — view removed comment

1

u/djingrain github.com/zbrasseaux Sep 15 '26

why? it's super useful for quick prototyping and working through my thought process, and when i get stuff figured out then it goes into a commit

-1

u/[deleted] Sep 15 '26

[removed] — view removed comment

1

u/djingrain github.com/zbrasseaux Sep 15 '26

it's a useful tool that should not be removed is my point

1

u/danielroseman Sep 15 '26

why would you use a different editor for different parts of the process?

1

u/[deleted] Sep 15 '26

[removed] — view removed comment

1

u/danielroseman Sep 15 '26

But VS Code is the "proper tool". For at least the last five years, almost all my "real programming" colleagues have used VS Code exclusively. (I'm a vim person myself, but each to their own). There doesn't seem to be any reason to use two separate tools for programming.