| | |
old-style class
![]() |
•
•
Join Date: Feb 2007
Posts: 19
Reputation:
Solved Threads: 0
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
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
You define method __str__() within the class:
python Syntax (Toggle Plain Text)
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
drink her pretty
•
•
Join Date: Feb 2007
Posts: 19
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
•
•
•
•
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__']
>>>
But that's boring. The 'real' difference between the styles is that new-style classes let you define public properties like this:
Python Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- Which browser do you design for? (HTML and CSS)
- style tag problem with XP (HTML and CSS)
- I just can't get this Javascript (JavaScript / DHTML / AJAX)
- linked stack question (C++)
Other Threads in the Python Forum
- Previous Thread: Help undestanding popen3 function
- Next Thread: Multi-dimensional Arrays:
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui heads homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython






