I'M reading a book on Python and my very first object/constructor program isn't show the output that the book is showing. I lacks the two lines "A new critter has been born!"

# Constructor Critter
# Demonstrates constructors

class Critter(object):
    """A virtual pet"""
    def _init_(self):
        print "A new critter has been born!"
        
    def talk(self):
        print "\nHi. I'm an instance of class Critter."
        
# main
crit1 = Critter()
crit2 = Critter()

crit1.talk()
crit2.talk()

raw_input("\n\nPress the enter key to exit.")

So what are objects all about anyway? It looks to me like a long complicated way of walling a function or method.

Recommended Answers

All 5 Replies

To answer the coding problem, I think that the word "init" has to be wrapped in double underscores.

A common mistake for beginners,
the __init__() constructor has two leading and two trailing __

In Python there is no need to use OOP classes, but you learn the benefits quickly as you write larger programs.

So what are objects all about anyway? It looks to me like a long complicated way of walling a function or method.

Objects are the result of years of programming practice by thousands of programmers. You need some time to see the benefit. The idea is that an object holds a certain quantity of data. The object's methods are functions which purpose is to manipulate the object's data.

Oh okay, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.