Python, How to use part of function outside function
Question: How can I use `today` and `returntime` in `return_fee` function?

    
    
    import datetime
    
    class Movie(object):
     def __init__(self,title):
        self.title = title
    
     def time_of_return(self):
        self.today = today
        self.returntime = returntime
    
        today = datetime.datetime.now()
    
        returntime = today + datetime.timedelta(days=30)
    
     def return_fee(Movie):
        fee = -2
        delta = today - returntime
    


Answer: If you want `time_of_return` and `return_fee` to be instance attributes, call
`time_of_return` from `__init__` to set them and then prefix with `self`:

    
    
    class Movie(object):
        def __init__(self,title):
            self.title = title
            self.time_of_return()
    
        def time_of_return(self):
            self.today = datetime.datetime.now()
            self.returntime = today + datetime.timedelta(days=30)
    
         def return_fee(Movie):
             fee = None
             delta = self.today - self.returntime
             # ... presumably do something else
    

Alternatively (since, in particular, `today` may change over time), call the
function `time_of_return` from within `return_fee` and make sure it returns
something:

    
    
    class Movie(object):
        def __init__(self,title):
            self.title = title
    
        def time_of_return(self):
            today = datetime.datetime.now()
            returntime = today + datetime.timedelta(days=30)
            return today, returntime
    
         def return_fee(Movie):
             fee = None
             today, returntime = self.time_of_return()
             delta = today - returntime
             # ... presumably do something else
    

It's a good idea to indent your code by 4 spaces, by the way. And `None` (or
0) would be a better default value for `fee`.

