old-style class

Reply

Join Date: Feb 2007
Posts: 19
Reputation: alba07 is an unknown quantity at this point 
Solved Threads: 0
alba07 alba07 is offline Offline
Newbie Poster

old-style class

 
0
  #1
Feb 19th, 2007
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
Last edited by alba07; Feb 19th, 2007 at 9:34 am. Reason: i did no mean to submitt it yet
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,542
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 171
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: old-style class

 
0
  #2
Feb 19th, 2007
You define method __str__() within the class:
  1. class Book:
  2. def __init__(self, author, title, price):
  3. self.author = author
  4. self.title = title
  5. self.price = price
  6.  
  7. def __str__(self):
  8. # overrides print for the instance
  9. text = "%s by %s" % (self.title, self.author)
  10. return text
  11.  
  12.  
  13. abook = Book(author = "Amy Doe", title = "Sun rise", price = 13.50)
  14.  
  15. print abook
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 19
Reputation: alba07 is an unknown quantity at this point 
Solved Threads: 0
alba07 alba07 is offline Offline
Newbie Poster

Re: old-style class

 
0
  #3
Feb 19th, 2007
Thanks.

I got it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 19
Reputation: alba07 is an unknown quantity at this point 
Solved Threads: 0
alba07 alba07 is offline Offline
Newbie Poster

Re: old-style class

 
0
  #4
Feb 19th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,542
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 171
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: old-style class

 
0
  #5
Feb 19th, 2007
Yes, the new class style would be
class Book(object):
where object is a place holder for possible inheritance from other class objects.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: old-style class

 
0
  #6
Feb 22nd, 2007
Originally Posted by IDLE
>>> help(object)
Help on class object in module __builtin__:

class object
| The most base type

>>> dir(object)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__']
>>>
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:

  1. class MenuItem(object):
  2.  
  3. def __init__(self, name, price):
  4. self.name = name
  5. self.price = price
  6.  
  7. def get_pricestr(self):
  8. return "$%0.2f" % self.price
  9.  
  10. def set_pricestr(self, price):
  11. self.price = float(price.strip('$'))
  12.  
  13. pricestr = property(get_pricestr, set_pricestr)
  14.  
  15. >>> m = MenuItem('flounder', 2.5)
  16. >>> print m.pricestr
  17. $2.50
  18. >>> m.pricestr = "$3.50" # inflation!
  19. >>> print m.price
  20. 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC