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

  • Home
  • SEARCH
  • 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 5952585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:42:09+00:00 2026-05-22T17:42:09+00:00

I have a bunch of variables that are equal to values pulled from a

  • 0

I have a bunch of variables that are equal to values pulled from a database. Sometimes, the database doesn’t have a value and returns “NoneType”. I’m taking these variables and using them to build an XML file. When the variable is NoneType, it causes the XML value to read “None” rather than blank as I’d prefer.

My question is: Is there an efficient way to go through all the variables at once and search for a NoneType and, if found, turn it to a blank string?

ex.

from types import *

[Connection to database omitted]

color = database.color
size = database.size
shape = database.shape
name = database.name
... etc

I could obviously do something like this:

if type(color) is NoneType:
  color = ""

but that would become tedious for the 15+ variables I have. Is there a more efficient way to go through and check each variable for it’s type and then correct it, if necessary? Something like creating a function to do the check/correction and having an automated way of passing each variable through that function?

  • 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-22T17:42:10+00:00Added an answer on May 22, 2026 at 5:42 pm

    All the solutions given here will make your code shorter and less tedious, but if you really have a lot of variables I think you will appreciate this, since it won’t make you add even a single extra character of code for each variable:

    class NoneWrapper(object):
        def __init__(self, wrapped):
            self.wrapped = wrapped
    
        def __getattr__(self, name):
            value = getattr(self.wrapped, name)
            if value is None:
                return ''
            else:
                return value
    
    mydb = NoneWrapper(database)
    color = mydb.color
    size = mydb.size
    shape = mydb.shape
    name = mydb.name
    
    # All of these will be set to an empty string if their
    # original value in the database is none
    

    Edit

    I thought it was obvious, but I keep forgetting it takes time until all the fun Python magickery becomes a second nature. 🙂 So how NoneWrapper does its magic? It’s very simple, really. Each python class can define some “special” methods names that are easy to identify, because they are always surrounded by two underscores from each side. The most common and well-known of these methods is __init__(), which initializes each instance of the class, but there are many other useful special methods, and one of them is __getattr__(). This method is called whenever someone tries to access an attribute. of an instance of your class, and you can customize it to customize attribute access.

    What NoneWrapper does is to override getattr, so whenever someone tries to read an attribute of mydb (which is a NoneWrapper instance), it reads the attribute with the specified name from the wrapped object (in this case, database) and return it – unless it’s value is None, in which case it returns an empty string.

    I should add here that both object variables and methods are attributes, and, in fact, for Python they are essentially the same thing: all attributes are variables that could be changed, and methods just happen to be variables that have their value set to a function of special type (bound method). So you can also use getattr() to control access to functions, which could lead to many interesting uses.

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

Sidebar

Related Questions

I have a matlab .mat file that stores a bunch of variables. How do
I have a bunch of variables that I want to check, and if they
I have a bunch of database addresses and other random long strings that I
week DKK id=radio2 value=75 /> I have bunch of these div entry generated by
I have a bunch of files that I need to be able to transport
I have a bunch of latitude/longitude pairs that map to known x/y coordinates on
I have a bunch of .NET frameworks installed on my machine. I know that
I have a bunch of perfmon files that have captured information over a period
I have a bunch (hundreds) of files that are supposed to have Unix line
I have a bunch of variables named index1, index2, ..., indexn. I want to

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.