0

I would like to make a function similar to the built-in functions for strings in python.

For example: foo.upper()

I would not like to have to make an object to do this, just change the string with a simple function within a class.

Here is what the code would be similar to

class autokey():
   def encrypt(self, string, key):
   # * Code to encrypt message *
   return encrypted_message

string = "test"
print(string.autokey.encrypt())
  • 1
    Maybe this answer can help you https://stackoverflow.com/a/15975791/9741875 – ferhen Mar 17 '20 at 06:03
  • So you want to be able to redefine the built-in string type? Why not just pass a string to the constructor of your class that has all the added features you need. You can imitate some builtin-in-like behaviors by overriding magic methods. These are the methods with double underscore (dunder). You can google python data model to get the python.org page on them. – Todd Mar 17 '20 at 06:10
  • Does this answer your question? [Extension method for python built-in types](https://stackoverflow.com/questions/6738987/extension-method-for-python-built-in-types) – errorau Mar 17 '20 at 06:33

0 Answers0