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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:07:30+00:00 2026-05-15T12:07:30+00:00

This is more of a curiosity question than anything else. I’m new with Python

  • 0

This is more of a curiosity question than anything else. I’m new with Python and playing around with it. I’ve just looked at the base64 module. What if instead of doing:

import base64
string = 'Foo Bar'
encoded = base664.b64encode

I wanted to do something like:

>>> class b64string():
>>>   <something>
>>>
>>> string = b64string('Foo Bar')
>>> string
'Foo Bar'
>>> string.encode64()
'Rm9vIEJhcg=='
>>> string
'Rm9vIEJhcg=='
>>> string.assign('QmFyIEZvbw==')
>>> string
'QmFyIEZvbw=='
>>> string.b64decode()
'Bar Foo'
>>> string
'Bar Foo'

Is there a simple, pythonic way to create that class?

I’ve begun with this:

>>> class b64string(base64):
...   def __init__(self, v):
...     self.value=v

And already I get:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
    module.__init__() takes at most 2 arguments (3 given)

And don’t get me started on (just to see what would happen):

>>> class b64string(str, base64): pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
    metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

I know how to do it manually by listing all of the attributes of base64 in a new class and calling them with the stored value as argument. But is there a neat, pythonic way to do this? Is it a bad idea to do it? The idea would be, if needed, to do it with many such modules and have “super strings” that would have as modules all the things I would need to do with them. Is that bad? Is it un-pythonic? If it is pythonic, how is it done?

  • 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-15T12:07:31+00:00Added an answer on May 15, 2026 at 12:07 pm

    I don’t think creating such complex string-like classes is a good idea, but if you really want to, here’s a simple snippet that runs your examples.

    First, we define a class that’s a generic string-wrapper. Its core is a __getattr__ function that forwards every method call to a given self.module, adding self.string as the first parameter and remembering the result on self.string.

    import base64
    
    class ModuledString(object):
        def __init__(self, string):
            self.string = string
    
        def __getattr__(self, attrname):
            def func(*args, **kwargs):
                result = getattr(self.module, attrname)(self.string, *args, **kwargs)
                self.string = result
                return result
            return func
    
        def __str__(self):
            return str(self.string)
    

    Creating a string-wrapper with base64 capabilities is then easy:

    class B64String(ModuledString):
        module = base64
    
    if __name__ == '__main__':
        string = B64String('Foo Bar')
        print string
        # 'Foo Bar'
        print string.b64encode()
        # 'Rm9vIEJhcg=='
        print string
        # 'Rm9vIEJhcg=='
        string.string = 'QmFyIEZvbw=='
        print string
        # 'QmFyIEZvbw=='
        print string.b64decode()
        # 'Bar Foo'
    

    Note that the above examples work only because b64encode and b64decode take a string as the first argument and return a string as the result (there is no validation in my __getattr__ function). A random function from some random module would probably raise some kind of exception. So, after all, it would be better to restrict the usage to a predefined set of functions from a given module, but it should be easy now.

    I repeat, I don’t recommend using such code in any serious project, only for fun.

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

Sidebar

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.