0

I have this function:

def check_if_year(years):
    if years.isnumeric():
        year = int(years)

How would I recall year in that function to use it outside of the function and into another function? Thanks in advance.

0

7 Answers 7

3

You should recall that function using return

def check_if_year(years):
    if years.isnumeric():
        year = int(years)
        return year

This returns/recalls year

Sign up to request clarification or add additional context in comments.

2 Comments

If years might be multiple years in, say, a list, he could be looking to use yield and a generator.
@DavidEhrmann but his want is to only use year outside the function say as a variable etc..
0
def check_if_year(years):
    if years.isnumeric():
        year = int(years)
        return year

Comments

0

You should return from the function:

def check_if_year(years):
    if years.isnumeric():
        return int(years)

You can then assign year as:

year = check_if_year(somestring)

outside that function.

3 Comments

try: file_name_one = input("First file name: ") file_stream = open(file_name_one, "r") for line in file_stream: year_str = line[:4] years = year_str check_if_year(years) year = check_if_year(years) check_leap_year(year) i get an attribute error
so i did year = check_if_year(years) then check_leap_year(year) which is my second function but i get an attribute error
It would be helpful to provide the full traceback - is check_leap_year also assuming year will be a string? E.g. do you call isdigit() again?
0

You can return the value of year as input to another funcion.

def check_if_year(years):
    if years.isnumeric():
        year = int(years)
        return year # function returns year 

y = check_if_year(years) # Call the check_if_year function and store the result in y

another_function(y) # Pass the return value year to another function 

Comments

0

Your function is actually mostly useless - what are you going to do if year is not numeric anyway ?

The simplest solution is to just try to build an int from whatever year is and handle the exception (how you want to handle it depends on the context) :

try:
    year = int(year)
except ValueError as e:
    handle_the_problem_one_way_or_another(year, e)
else:
    call_some_other_func(year)

Comments

0
def remember_year( year ): # Return a function that 'remembers' the value of year
    return lambda x: x + year

plus10 = remember_year( 10 )
print plus10( 5 )
>> 15

Comments

0

I would propose that EAFP is more Pythonic than the LBYL approach you have:

def check_if_year(years):
    try:
        return int(years)
    except ValueError as e:
        print e
        # do something with the fact you have a bad date...

Interact this way:

>>> check_if_year('1999')
1999
>>> check_if_year('abc')
invalid literal for int() with base 10: 'abc'

Then if you wanted to 'put inside another function' you could do something like this:

def leap_year(st):
    def check_if_year(year):
        try:
            return int(year)
        except ValueError as e:
            raise

    n=check_if_year(st)
    if n % 400 == 0:
        return True
    if n % 100 == 0:
        return False
    if n % 4 == 0:
        return True
    # else    
    return False        

print leap_year('1900')        # False
print leap_year('2000')        # True
print leap_year('next year')   # exception  

4 Comments

your except clause is worse than useless.
@brunodesthuilliers That is kinda harsh. It is just an ignored except clause. (Or at least one that only prints the exception as an example.) Who says that an except needs to do anything? It is a simple example to show the OP an alternate way to code a simple function. What would YOU have the except do in this case? It is up to the OP what is to be done with a bad year string.
"Errors should never pass silently." (Zen of Python) - and I've lost days debugging bad code that silently swallowed exceptions or assumed too much about what went wrong exactly. Remember that your answer will be read by a lot of newcomers that may understand it as being a good practice - which it's not. wrt/ "What would I do", it really depends on the context - either let the exception propagate, wrap it into another more specific exception, or something more complex (like in the case of a HTTP form validation), but I would certainly not just print the exception and call it a day.
@brunodesthuilliers: This is not meant to be production code. It is a simple example of try/except that the OP can choose to learn more about or not. Since what is to be done with a bad year string is not specified in the original, it makes no sense to specify it here. The second example I added (based on the OP's comments) does exactly what you say it should: die on a bad string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.