Giter Site home page Giter Site logo

class-and-sub-class's Introduction

import datetime

class Person(object): def init(self, name): """create a person called name""" self.name = name self.birthday = None self.lastName = name.split(' ')[-1]

def getLastName(self):
    """return self's last name"""
    return self.lastName

def setBirthday(self,month,day,year):
    """sets self's birthday to birthDate"""
    self.birthday = datetime.date(year,month,day)

def getAge(self):
    """returns self's current age in days"""
    if self.birthday == None:
        raise ValueError
    return (datetime.date.today() - self.birthday).days

def __lt__(self, other):
    """return True if self's ame is lexicographically
       less than other's name, and False otherwise"""
    if self.lastName == other.lastName:
        return self.name < other.name
    return self.lastName < other.lastName

def __str__(self):
    """return self's name"""
    return self.name

me = Person("William Eric Grimson")

print me

me.getLastName()

me.setBirthday(1,2,1927)

me.getAge()

her = Person("Cher")

her.getLastName()

plist = [me, her]

for p in plist: print p

plist.sort()

for p in plist: print p

class MITPerson(Person): nextIdNum = 0 # next ID number to assign

def __init__(self, name):
    Person.__init__(self, name) # initialize Person attributes
    # new MITPerson attribute: a unique ID number
    self.idNum = MITPerson.nextIdNum
    MITPerson.nextIdNum += 1

def getIdNum(self):
    return self.idNum

# sorting MIT people uses their ID number, not name!
def __lt__(self, other):
    return self.idNum < other.idNum

p1 = MITPerson('Eric') p2 = MITPerson('John') p3 = MITPerson('John') p4 = Person('John')

print p1 print p1.getIdNum() print p2.getIdNum()

class-and-sub-class's People

Contributors

taimoorsattar7 avatar

Stargazers

Hadj H. avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.