Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7050133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:06:23+00:00 2026-05-28T03:06:23+00:00

I am carrying out code maintenance on an old Python script. I have come

  • 0

I am carrying out code maintenance on an old Python script. I have come accross a piece of the code, which has me flumoxed. Earlier on in the code (not shown in the snippet below), class are defined, with minimal attributes. Further on in the code, assignments are made to non existent fields in the class. For example

tempvar = MyClassObject()
tempvar.this_field_was_not_defined_in_the_class = 42

Later on in the script, the variable tempvar is written to a CSV file by calling:

write_csv('test.csv', tempvar, ('declared_field1', 'declared_field2',
                                ('declared_field3', 'New Label'), 
                                'this_field_was_not_defined_in_the_class') )

Here is the (confusing [to me]) function that writes the object to file:

def write_csv(filename, objects, fields, add_weightings=True):
    # The items in fields can either be a tuple of the attribute name and a label for that
    # attribute, or just an attribute name. In the later case replace it with a tuple with an
    # automatically generated label.
    fields = list(fields)
    for i in xrange(len(fields)):
        if isinstance(fields[i], tuple):
            continue
        else:
            fields[i] = (fields[i], fields[i].replace('_', ' '))

    with open(filename, 'wb') as f:
        f.write(codecs.BOM_UTF8)
        c = csv.DictWriter(f, [i[0] for i in fields])
        c.writerow(dict(fields))
        c.writerows(
            [utf8ify(add_weighting(i.__dict__) if add_weightings else i.__dict__) for i in objects])

Can anyone explain what is going on? BTW utf8ify and add_weighting are global functions defined in the script.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:06:24+00:00Added an answer on May 28, 2026 at 3:06 am

    There are no “non-existent” attributes of an object. There are no declarations.

    Attributes are merely assigned to objects in __init__ or in any code that references the object. That’s just standard Python.

    class Whatever( object ):
        pass
    
    w = Whatever()
    w.new_attribute= "some value"
    

    Perfectly normal. The attribute new_attribute is not “non-existent” because it doesn’t need any kind of declaration.

    When you do

    >>> w.__dict__
    

    at the interactive prompt, you’ll see where attributes live.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After carrying out a lot of research I have come to the conclusion that
I have just recently started carrying out unit testing for my software written in
Carrying on from my previous question, which has now been solved: Issue with Pointers
I'm carrying out some experiments in theorem proving with combinator logic, which is looking
Say I have a batch file for carrying out a long build and at
I am just carrying out a query, and have started using parameters. And all
I have a piece of Verilog code worked upon by a programmer no longer
I have this API - which I managed to carryout some API steps solely
How would you model a system that handles permissions for carrying out certain actions
I figure out I can have the implementation of parts of a class in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.