Generated Python __str__ Method Format and Calling Parameters

In addition to the print_value method described above, generated Python code also includes the usual Python __str__ method. The presence of this method allows you to manipulate a generated object as a string. Using the personnelRecord example from above:

print(personnelRecord)

or:

         prstring = str(personnelRecord)
         print(prstring)
      

The output of either of these print commands would be as follows:

         {
            name {
               givenName = 'John'
               initial = 'P'
               familyName = 'Smith'
            }
            number = 51
            title = 'Director'
            dateOfHire = '19710917'
            nameOfSpouse {
               givenName = 'Mary'
               initial = 'T'
               familyName = 'Smith'
            }
            children[0] {
               name {
                  givenName = 'Ralph'
                  initial = 'T'
                  familyName = 'Smith'
               }
               dateOfBirth = '19571111'
            }
            children[1] {
               name {
                  givenName = 'Susan'
                  initial = 'B'
                  familyName = 'Jones'
               }
               dateOfBirth = '19590717'
            }
         }
      

You can see that the output is virtually the same as the output from the print_value call, except there is no name assigned to the entire grouping.

The __str__ method is called by the generated print_value method, but it can also be called directly by you if desired. Its signature is as follows:

   def __str__(self, elem_name=None, indent_level=None):

The elem_name argument is a name to assign to the listing, and the indent_level argument controls the indentation of the outermost level of the listing. Each indent level results in an indentation of 3 spaces. So, again using the personnelRecord example, you can do this:

         prstring = personnelRecord.__str__("ThisRecord", 1)
         print(prstring)
      

The output of the print command would be as follows:

            ThisRecord {
               name {
                  givenName = 'John'
                  initial = 'P'
                  familyName = 'Smith'
               }
               number = 51
               title = 'Director'
               dateOfHire = '19710917'
               nameOfSpouse {
                  givenName = 'Mary'
                  initial = 'T'
                  familyName = 'Smith'
               }
               children[0] {
                  name {
                     givenName = 'Ralph'
                     initial = 'T'
                     familyName = 'Smith'
                  }
                  dateOfBirth = '19571111'
               }
               children[1] {
                  name {
                     givenName = 'Susan'
                     initial = 'B'
                     familyName = 'Jones'
                  }
                  dateOfBirth = '19590717'
               }
            }
      

So in this case the output is all indented 3 spaces beyond the indentation from the previous examples, and the name assigned to the listing is "ThisRecord".