Monday, September 21, 2015

Create an object in Python

You can create an object as follows:

class DiskSpace(object):
  def __init__(self, total, free):
    self.total = total
    self.free = free

  def __str__(self):
    return "total: %d, free: %d" % (self.total, self.free)

diskSpace = DiskSpace(100, 50)
print diskSpace

References:
http://stackoverflow.com/questions/15081542/python-creating-objects
http://stackoverflow.com/questions/727761/python-str-and-lists
http://www.pythonforbeginners.com/concatenation/string-concatenation-and-formatting-in-python

No comments:

Post a Comment