0

I have a static method in a class being called by another static method of that class and returning variables that it is supposed to create, but it seems it is creating global variables and re-returning them when it should be returning None

class LineHandler:

    @staticmethod
    def file_tocsv(_file):
        line_number=0
        csv_dict = {}
        for linex in _file:
            linex_dict = Utils._to_utf(kline)
            csv_dict, line_number = LineHandler.line_tocsv(csv_dict, linex_dict, line_number)

    @staticmethod
    def line_tocsv(csv_dict={}, line={}, current_line=0):
        csv_line, current_line = LineHandler.create_csv(current_line, kline)
        if current_line in csv_dict:
            csv_dict[current_line].update(csv_line)
        else:
            csv_dict[current_line] = csv_line 
        return csv_dict


    @staticmethod
    def create_csv(line_number, kline={}, csv_line={}):
        #retval dict 
        bracket = kline.get('][',None)
        text = kline.get('line',None)
        star = kline.get('*',None)
        at = kline.get('@',None)

        string = ''; string2 = ''
        if star:
            string2, csv_line, is_newline = LineHandler._star( star, string2, csv_line) 
            line_number += is_newline
        if at:
            csv_line = LineHandler._effect(at, csv_line) 
        if bracket and text:
            csv_line = LineHandler._text(bracket, csv_line, text) 
        return csv_line, line_number

as i said, as soon as it gets to the 3rd functtion, the csv_line var is still there instead of becoming {} !!

tenshiism
  • 57
  • 1
  • 5
  • You could use non conflicting variable names. But that does seem weird. I have never ran into that problem – OneCricketeer Feb 08 '16 at 04:48
  • Where are you getting `csv_line` from in the second method? – Hugh Bothwell Feb 08 '16 at 04:49
  • 1
    Are you sure this isn't just because you set a mutable default (`csv_line={}`)? Should instead be `csv_line=None` then `if csv_line is None: csv_line = {}` inside the method, otherwise changes to `csv_line` will carry over between calls. – Hugh Bothwell Feb 08 '16 at 04:52
  • 2nd is geting it from the 3rd. forgot to include that line. its there now – tenshiism Feb 08 '16 at 05:41
  • why will the changes carry over? i thought all method variables were deleted every time a method was called? if they arent how will setting a different default change anything? – tenshiism Feb 08 '16 at 05:42
  • You're using a mutable default argument. It is passed by reference, so the reference is kept around for the dictionary – OneCricketeer Feb 08 '16 at 05:43
  • so if csv_line reference is a dict and i make the mutable default '=None' then that wont do ANYthing will it? i need to be able to pass the line back between the 2nd and the 3rd method so i can add to it or else i cant update the file dictionary (passing into 3rd not implemented here yet fyi) so i cant force it to none either – tenshiism Feb 08 '16 at 05:46
  • You could add an if None check to initialize it, but I don't think that is proper variable initialization. I could work that way, I've never tried that – OneCricketeer Feb 08 '16 at 05:47
  • can someone tell me if mutable defaults work the same for if the method call included the var as if the var existed as a reference but wasnt passed to the method ? and then how i can avoid that? – tenshiism Feb 08 '16 at 05:50
  • oh! could i put the function in an other class and then call the class which would make a new class instance wherein the method is redefined and thus the mutable is reinitialised!? or would it just call the same class instance with the same function with the same mutable attribute? (it is an attribute, right?) – tenshiism Feb 08 '16 at 05:57

0 Answers0