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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:08:31+00:00 2026-06-17T14:08:31+00:00

I apologize for the newbie question, but this is my first time working with

  • 0

I apologize for the newbie question, but this is my first time working with classes. The class I’m trying to create is intended to perform a regex find and replace on all keys and values within a dictionary. The specific find and replace is defined upon instantiation.

There are two issues that I have. The first issue is that each instance of the class needs to accept a new dictionary. I’m not clear on how to create a class that accepts a general dictionary which I can specify upon creating an instance.
The second issue is that the class I have simply isn’t working. I’m receiving the error message TypeError: expected string or buffer in the class line v = re.sub(self.find,self.replace,v).

There are three instances I want to create, one for each input dictionary: input_iter1, input_iter2, and input_iter3.

The following is the class:

class findreplace:
    values = []
    keys = []
    def __init__(self, find, replace):
        self.find = find
        self.replace = replace
    def value(self):
        for k,v in input_iter1.items():
            v = re.sub(self.find,self.replace,v)
            findreplace.values.append(v)
    def key(self):
        for k,v in input_iter1.items():
            k = re.sub(self.find,self.replace,k)
            findreplace.keys.append(k)

The following are the instances:

values1 = findreplace('[)?:(]','')
values1.value()
values2 = findreplace(r'(,\s)(,\s)(\d{5})({e<=1})',r'\2\3')
values2.value()
keys1 = findreplace(r'(?<=^)(.+)(?=$)',r'(?:\1)')
keys1.key()
keys2 = findreplace(r'(?=$)',r'{e}')
keys2.key()

print values
print keys

If anyone has any insight on how I can workaround these two issues, I’d be grateful to hear them. Thanks!

  • 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-06-17T14:08:31+00:00Added an answer on June 17, 2026 at 2:08 pm

    First, Python 2 classes should start off this way:

    class Foo(object):
    

    Otherwise, you get an “old-style class”, which is some ancient crusty thing no one uses.

    Also, class names in Python are typically written in CamelCase.

    Second, do not use mutable values (like lists!) as class attributes, as you’re doing here with keys and values. They’ll be shared across all instances of your class! It looks like you’re even aware of this, since you refer to findreplace.keys directly, but it doesn’t make sense to store instance-specific values in a class attribute like that.

    But, most importantly: why is this a class at all? What does a findreplace represent? It looks like this would be much clearer if it were just a single function.


    To answer your actual questions:

    1. You pass in a dictionary just like you’re passing in find and replace. Add another argument to __init__, and pass another argument when you construct your class.

    2. Presumably, you’re getting the TypeError because one of the values in your dictionary isn’t a string, and you can only perform regexes on strings.

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

Sidebar

Related Questions

I apologize if this is a newbie question. I have been working on trying
First I apologize if this is a fairly simple question, but I am a
I apologize if this is a newbie question, but I cannot figure out why
I apologize for this newbie question, but I'm looking for a simple solution. I
I apologize for the newbie question, but I am struggling with this problem. I
First : Appologize for my bad english. Sorry for this newbie software question, but
I'm pretty new to Maven, so I apologize if this is a newbie question.
I apologize in advance, if this question doesn't make sense. I'm a newbie. I
I apologize in advance for the newbie nature of this question. Here is my
I'm a python newbie so I apologize in advance if this question has been

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.