Hi, I'm still practice learning Python.
I have a litte homework, make Class method,
Class Date and Class Delta without using import.
it must be like this...

>>> date(2020, 5, 2) + delta(year=5)
date(2025, 5, 2)

>>> date(2020, 5, 2) + delta(month=3)
date(2020, 8, 2)

>>> date(2020, 5, 2) + delta(day=10)
date(2020, 5, 12)

>>> date(2020, 5, 2) + delta(year=5, month=7)
date(2025, 12, 2)

must be tuple and without import.
I'm using python2.7
If i using two init or two call
place one at class date and class delta.
the argumen always take
'TypeError'

Thanks Before..

Post your code. You don't need __call__() methods, you only need one __init__() method per class, and also __add__() and perhaps __radd__() methods.

Edited 7 Months Ago by Gribouillis

here's my code.

class Date():

    class year:
        def __int__(self, year):
            return 2020

    class month:
        def __int__(self, month):
            return 5

    class day:
        def __int__(self, day):
            return 2

    def __call__(self, year, month, day):
        return year, month, day

class Delta():

    class year:
        def __int__(self, year):
            return 0

    class month:
        def __int__(self, month):
            return 0

    class day:
        def __int__(self, day):
            return 0

    def __add__(self, year=0, month=0, day=0):
        return (year, month, day)

if i place add without init at class
always TypeError: object () takes no parameter

Edited 7 Months Ago by templar77

Like this?

class Date:

    def __init__(self, year, month, day):
        self.year=year
        self.month=month
        self.day=day
    def __add__(self, era):
        year=self.year+era.year
        month=self.month+era.month
        day=self.day+era.day

class Delta:
    def __add__(self, era):
       return self+era

date = Date()
delta = Delta()

It is much better for class Date, but you must create an instance with parameters, for example

date = Date(2016, 4, 29)

There should be a __init__()method for class Delta too, with convenient parameters.

how to make parameter? i'm still confusing using init and add function

For Delta, you need optional parameters. For this you can define

def __init__(self, year=1970, month=1, day=1)

for example.

Ok i'm change to be like this

class Date:

    def __init__(self, year, month, day):
        self.year=year
        self.month=month
        self.day=day


    def __add__(self, era):
        year=self.year+era.year
        month=self.month+era.month
        day=self.day+era.day



class Delta:
    def __init__(self, year=0, month=0, year=0):
        self.year=year
        self.month=month
        self.day=day

    def __add__(self, era):

       return self+era





date = Date()

delta = Delta()

but syntax error, `SyntaxError: duplicate argumen 'year' in function defeinition

Look at the whole error message, it tells you that the error is at line 17, because there are 2 paremeters named yearin your __init__() method.

Ahh i see, sorry for that. I fix the code, became like this...

class Date:

    def __init__(self, year, month, day):
        self.year=year
        self.month=month
        self.day=day

    def __add__(self, era):
        year=self.year+era.year
        month=self.month+era.month
        day=self.day+era.day



class Delta:

    def __init__(self, year=1978, month=5, day=3):
        self.year=year
        self.month=month
        self.day=day
    def __add__(self, era):
       return self+era

date = Date()
delta = Delta()

But the code, takes TypeError like i said above, __init__ takes exactly 4 argumen (1 given)

Sorry for late, I don't think this right code..

class Date:
    def __init__(self, year=2020, month=6, day=2):
        self.year=year
        self.month=month
        self.day=day
    def __str__(self):
        return '(' + str(self.year) + ', ' + str(self.month) + ', '+ str(self.day) + ')'   
    def __add__(self, era):
        year=self.year+era.year
        month=self.month+era.month
        day=self.day+era.day

or like this?

class Date:
    #new def for Date
    def Date(year, month, day):
        return 2020,6,2    

The first definition is correct. The __add__() method must return a value, you must add

return Date(year, month, day)

in this method.

Like this?

class Date:
    def __init__(self, year, month, day):
        self.year=year
        self.month=month
        self.day=day


    def __add__(self, era):
        year=self.year+era.year
        month=self.month+era.month
        day=self.day+era.day
        return Date(year, month, day)
        #or Date(2020, 6, 2)

Not yet master, still TypeError: __init__() exactly 4 argumen (1 given)

Please post the whole code.

If you write

 def __init__(self, year, month, day):

then you need 3 arguments (python says it's 4)

date = Date(2016, 5, 3)

If you write instead

def __init__(self, year=2000, month=1, day=1):

Then, the arguments are optional. You can use

date = Date(year=2016)

here the last code i write..

class Date:

    def __init__(self, year, month, day):

        self.year=year

        self.month=month

        self.day=day





    def __add__(self, era):

        year=self.year+era.year

        month=self.month+era.month

        day=self.day+era.day

        return Date(year, month, day)

        #or Date(2020, 6, 2)



class Delta:

    def __init__(self, year=1978, month=5, day=3):

        self.year=year

        self.month=month

        self.day=day

    def __add__(self, era):

       return self+era



def foo(Date, Delta):

    return Date + Delta





date = Date()

delta = Delta()

If i wrote

 date=Date(2020, 5, 2)

that error AttributeError: Date instance has no __call__ method

Edited 7 Months Ago by templar77

sorry i mean this the code like a instruction

class Date:

    def __init__(self, year, month, day):

        self.year=year

        self.month=month

        self.day=day





    def __add__(self, era):

        year=self.year+era.year

        month=self.month+era.month

        day=self.day+era.day

        return Date(year, month, day)

        #or Date(2020, 6, 2)



class Delta:

    def __init__(self, year=1978, month=5, day=3):

        self.year=year

        self.month=month

        self.day=day

    def __add__(self, era):

       return self+era



date = Date()

delta = Delta()

the problem solved, i try change the code like this...

class date(object):
    def __init__(self, year, month, day):
        self.year=year
        self.month=month
        self.day=day    
    def __add__(self, era):
        year=self.year+era.year
        month=self.month+era.month
        day=self.day+era.day
        return date(year, month, day)

    def __str__(self):
        return "("+",".join(map(str,(self.year,self.month,self.day)))+")"

class delta(object):
    def __init__(self, year=1978, month=5, day=3):
        self.year=year
        self.month=month
        self.day=day

    def __add__(self, era):
       return self+era


print date(2020,5,2)+delta(year=20, month=7)

Will be equal
date(2040, 11, 2)
Thanks for instruction, i'm glad meet u and this forum...

Cheers

There is still some work to do with the addition operation because something must be done if the month becomes larger than 12, or if the day becomes larger than the last day of the month.

This question has already been answered. Start a new discussion instead.