Recursive method not returning proper values

I have created a method to return the maximum lengths of the keys and values returned by calling vars(object). I can see that it iterates properly but returns the values in effect prior to recursion.

def get_max_lengths(an_object, key_length, value_length):
    a_dict = vars(an_object)
    max_key_length = key_length
    max_value_length = value_length
 
    for key, value in a_dict.items():
        if str(value).find("__main__.") > 0 and str(value).find(" object at ") > 0: 
         #-> Recurse this object to measure its variables
            get_max_lengths(value, max_key_length, max_value_length) 
        else:
            if len(str(key)) > max_key_length:
               max_key_length = len(key) 
               print((max_key_length, max_value_length))
            if len(str(value)) > max_value_length:
                 max_value_length = len(str(value))
                 print((max_key_length, max_value_length))
    
    return (max_key_length, max_value_length)

As I iterate the routine, it does accumulate the correct values but doesn’t return them

   Tims's Car before creating oil_specs:

: Variables :

: vin : Unknown :
: make : Hundai :
: model : Elantra :
: owner : Tim :
: year : 2017 :
: color : blue :

    Tims's Car after calling set_oil_capacity volume and units:

: Variables :

: vin : Unknown :
: make : Hundai :
: model : Elantra :
: owner : Tim :
: year : 2017 :
: color : blue :
: oil_specs : <main.Oil_Specs object at 0x000002036C4D5C90> :

    Tims's Car's Oil Spec:

: Variables :

: oil_capacity_volume : 5 :
: oil_capacity_units : quarts :
: oil_preferred_type : Synthetic :

(3, 0)
(3, 7)
(4, 7)
(5, 7)
(19, 7)
(19, 9) # It should return this tupple.

The field lengths returned from get_max_lengths are (5, 7)

You’ve written it so that it calls itself, but just discards the returned results when it does so.

Another possible problem I see is that .find actually returns -1 if the string doesn’t contain the substring - if the substring occurs at the start of the string, .find will return 0. If you’re checking for the presence of a particular substring, a clearer way is to use: substring in string.

Additionally, if value is already a string, there’s no need to use str(...).

Thank you for your quick response. I realize that I am discarding the returned results. My question is how to avoid doing so in a Pythonic manner. I also now realize that converting the key to a string is unnecessary as a class variable cannot be a number. However, in this case, the year is an integer so it must be converted.