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 6019265
In Process

The Archive Base Latest Questions

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

The code below works (it will eventually pull records from a database via SQL),

  • 0

The code below works (it will eventually pull records from a database via SQL), but I am having trouble understanding why the assert statement in the code below works.

def build_row(table, cols):
    """Build a class that creates instances of specific rows"""
    class DataRow:
        """Generic data row class, specialized by surrounding function"""
        def __init__(self, data):
            """Uses data and column names to inject attributes"""
            assert len(data)==len(self.cols)
            for colname, dat in zip(self.cols, data):
                setattr(self, colname, dat)
        def __repr__(self):
            return "{0}_record({1})".format(self.table, ", ".join(["{0!r}".format(getattr(self, c)) for c in self.cols]))
    DataRow.table = table
    DataRow.cols = cols.split()
    return DataRow

When is the length of ‘data’ determined and how is it guaranteed to be the same length as ‘self.cols’? Will someone please explain this behavior?

  • 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-23T03:23:47+00:00Added an answer on May 23, 2026 at 3:23 am

    This may make more sense to you if you replace the references to self.cols and self.table with self.__class__.cols and self.__class__.table. This confusion is caused by accessing class attributes through the self object as if they were instance attributes, and why I don’t particularly like this coding idiom. When looking at the code in __init__, where instance attributes are typically assigned their values, it is jarring to immediately see a statement that reads the value of self.cols – where the heck did self.cols get initialized?! On the other hand, if you change this assert to:

    assert len(data)==len(self.__class__.cols)
    

    then it becomes clearer that the list of data values is expected to be the same length as the defined list of columns for this instance of DataRow. The class attribute cols gets initialized right after the class definition, in this statement:

    DataRow.cols = cols.split()
    

    long before any instance gets created – that’s why this assert works.

    The writer of this code might consider converting to the more current namedtuple construct.

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

Sidebar

Related Questions

The code below works. But if I comment out the line Dim objRequest As
Will the code below work if the clock on the server is ahead of
So far i have got the code below which works lovely when trying an
See code below, for some reason it only works when I put a breakpoint
The code below is the code i am using. It works fine in thunderbird
In the code below, why does the open function work but the close function
I have the code below and it works pretty good except if you enter
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code below is not working as expected to detect if it is in design
The code below shows a sample that I've used recently to explain the different

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.