Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Methods
#1
Does anyone here knows how to do methods inside a class? 
I need help to find:

1)  a method to mark a book as completed

2 )a method to determine if the book is long (500 or more pages)

Like how do i write it?

Help is appreciated!

Update: i have wrote down the class. I just need that two method, but how do i write it?
class Book:
    def __init__(self, title="", author="", pages=0, status=""):
        self.title = title
        self.author = author
        self.pages = pages
        self.status = status

    def __str__(self):
       return "{} by {}, total pages is {}.".format(self.title, self.author, self.pages)
Reply
#2
You already wrote two methods (init and str), although they are special class methods. But you proceed exactly the same way with writing your methods. Just give them proper names like mark_read() and is_long(), or anything you prefer.
I just stumbled upon this
https://www.python.org/dev/peps/pep-0008...-variables
you may use it to follow a naming convention for coding in Python.
Reply
#3
(Jan-19-2017, 07:33 AM)j.crater Wrote: You already wrote two methods (init and str), although they are special class methods. But you proceed exactly the same way with writing your methods. Just give them proper names like MarkRead() and IsLong(), or anything you prefer.

Yeah, i gotta write the methods under one class.
What do i have to write under the methods MarkRead() and IsLong()? 
I'm a newbie on Python. ?
Reply
#4
mark_read() and is_long() were my suggestion for method names, but you can use anything. "Under" the methods (in method body, better put), you should write the code that you want executed by calling a particular method. It works much like functions.
If you are new to Python and have not yet learned about functions, I suggest you dive into them before moving to classes and methods. Things will make a lot more sense then and your learning will be efficient.
https://www.tutorialspoint.com/python/py...ctions.htm
Reply
#5
Hm! Your class actually just holds a data. Why not a dictionary?


book = {'title': "", 'author': "", 'pages': ""}

print("{} by {}, total pages is {}.".format(book[titile], book[author], book[pages])
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(Jan-19-2017, 07:53 AM)wavic Wrote: Hm! Your class actually just holds a data. Why not a dictionary?

It is the Homework section. Obviously it's a OOP class, even if it looks strange to teach OOP when s/he is not familiar with more basic concepts.
Reply
#7
(Jan-19-2017, 07:44 AM)j.crater Wrote: mark_read() and is_long() were my suggestion for method names, but you can use anything. "Under" the methods (in method body, better put), you should write the code that you want executed by calling a particular method. It works much like functions.
If you are new to Python and have not yet learned about functions, I suggest you dive into them before moving to classes and methods. Things will make a lot more sense then and your learning will be efficient.
https://www.tutorialspoint.com/python/py...ctions.htm

is this correct? :/
# a method to mark the book as completed
def mark_completed(self):
   if self.status == "c"
       self.title += "Mark as completed"
   elif self.status == "r"
       self.title += "Book is not completed"

# a method to determine if the book is long (500 or more pages)
def is_long(self):
   if self.pages > 500:
       return True
   return False
Edit admin use code tag.
Look at BBcode.
Reply
#8
this one
# a method to determine if the book is long (500 or more pages)
def is_long(self):
  if self.pages > 500:
      return True
  return False
is more or less correct, i.e. it could be done better, e.g.
# a method to determine if the book is long (500 or more pages)
def is_long(self):
  return self.pages >= 500
as to this one:
# a method to mark the book as completed
def mark_completed(self):
  if self.status == "c"
      self.title += "Mark as completed"
  elif self.status == "r"
      self.title += "Book is not completed"
I think you need to change status property and not change the book title.
Reply
#9
(Jan-19-2017, 08:25 AM)buran Wrote: this one
# a method to determine if the book is long (500 or more pages)
def is_long(self):
  if self.pages > 500:
      return True
  return False
is more or less correct, i.e. it could be done better, e.g.
# a method to determine if the book is long (500 or more pages)
def is_long(self):
  return self.pages >= 500
as to this one:
# a method to mark the book as completed
def mark_completed(self):
  if self.status == "c"
      self.title += "Mark as completed"
  elif self.status == "r"
      self.title += "Book is not completed"
I think you need to change status property and not change the book title.

how?  Confused
Reply
#10
Both status and title are properties of class Book. In your code you change one of these properties and I just suggest that you should change the other one.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020