Python: How would I compare two strings using a function with only one of the strings as an argument? -
i've created class, , unsure how approach following problem. possible create function able in example? (for practical uses, comparing dates , returning true if day , month same not years same)
example:
>>>strvar1 = 'abc-123' >>>strvar2 = 'abc-456' >>>strvar1.myfunction(strvar2) true
class code
class date(object): def __init__(self, x0 = 1900, y0 = 1, z0 = 1): self.x = x0 self.y = y0 self.z = z0 def __str__(self): date = str(self.x) + "-" + str(self.y).rjust(2, '0') + "-" + str(self.z).rjust(2, '0') return date def myfunction(j):
so example like:
d1 = date(1999, 1, 1) //d1 = "1999-01-01" d2 = date(2000, 2, 2) //d2 = "2000-02-02" d3 = date(2001, 2, 2) //d3 = "2001-02-02" >>>d1.myfunction(d2) false >>>d2.myfuction(d3) true
yes absolutely, reason having classes. read on https://docs.python.org/2/tutorial/classes.html.
def myfunction(self, cdate): return self.y == cdate.y , self.z == cdate.z
Comments
Post a Comment