I am to create an old-style class with a constructor __str__, and accessor methods, such that the following code:

abook = Book(author = "Amy Doe", title = "Sun rise", price = 13.50)
print abook.getAuthor()
print abook.getTitle()
print abook.Price()
print abook

Output should be as follows:
Amy Doe
Sun Rise
13.50
Sun Rise by Amy Doe

So far I only have:

def class(book):
def __init__(self, author, title, price):
self.author = Author
self.title = Title
self.price = Price

Where does the __str__ come in?

Please help, due tomorrow.

Thanks in advance

You define method __str__() within the class:

class Book:
    def __init__(self, author, title, price):
        self.author = author
        self.title = title
        self.price = price
        
    def __str__(self):
        # overrides print for the instance
        text = "%s by %s" % (self.title, self.author)
        return text
    

abook = Book(author = "Amy Doe", title = "Sun rise", price = 13.50)

print abook

Also, just so I know, what is the difference between old style classes and new style classes, for example in this case. All that I was ever tought about the new style classes is that they inherit from a type, like class C (dict): or Class X (object):

So if I wanted to change to book class to a new style class I would write it as follows:

class Book (object): ??? not sure if this is right though?

Thanks.

Yes, the new class style would be
class Book(object):
where object is a place holder for possible inheritance from other class objects.

>>> help(object)
Help on class object in module __builtin__:

class object
| The most base type

>>> dir(object)

>>>

object is the base class that is at the top of any inheritance tree. The purpose of insisting on the base class, I believe, is to unify object behavior without requiring too much 'magic.' That is, prior to new-style classes, objects just magically had properties like __doc__ and __str__; now, they have them for a reason: because they inherited them from the base class.

But that's boring. The 'real' difference between the styles is that new-style classes let you define public properties like this:

class MenuItem(object):
   
   def __init__(self, name, price):
        self.name = name
        self.price = price

   def get_pricestr(self):
        return "$%0.2f" % self.price
 
   def set_pricestr(self, price):
        self.price = float(price.strip('$'))
   
   pricestr = property(get_pricestr, set_pricestr)

>>> m = MenuItem('flounder', 2.5)
>>> print m.pricestr
$2.50
>>> m.pricestr = "$3.50"  # inflation!
>>> print m.price
3.5

From the outside, pricestr looks just like a normal property, as if it were just stored in the MenuItem object. But in reality, setting pricestr equal to something (like '$3.50') automagically calls the set_pricestr function with the value '$3.50', while reading the pricestr value automagically calls the get_pricestr function.

The result is cleaner code.

Jeff

This article has been dead for over six months. Start a new discussion instead.