0
why this program is showing error?
class book: def __init__(self,a,b,c,d,e): self.title=a self.author=b self.publisher=c self.price=d self.author_royality=e def showinfo(self): print('title:', self.title) print('author:', self.author) print('publisher:', self.publisher) print('price:', self.price) print('author_royality:', self.author_royality)
6 Réponses
+ 4
Doesn't really match your other question where it says "Provide getter and setter properties for all variables. Also define a method royalty() to calculate royalty amount author can expect to receive the following royalties (...)"
A getter method is defined like this:
@property
def price(self):
return self._price
A setter method is defined like this:
@price.setter
def price(self, price):
self._price = price
For the royalty class method, I'd make a static method as I think the method is the same for all books and you want to be able to call the method without referencing a specific instance of the class:
@staticmethod
def royalties(retail_price, copies_sold):
# calculate royalties here
return royalties
+ 4
You need to define getters and setters for all properties
+ 2
It won't show any error, everything seems fine. To make books you will need to define a new book and give it the attributes a,b,c,d,e
Example:
book1 = book("any_title", "any_author", "any_publisher", "any_price", "anyauthor_royality")
Then you can say: print(book1.showinfo())
+ 1
after making object and calling showinfo method it is showing bound error
+ 1
<bound method book.showinfo of <__main__.book object at 0x03066190>>
0
static method cannot modify the state of object but i also have to change the state of object