0

Im looking to create something that I can use to check if a string meets a condition like so:

var = "hello"
check = var.checkconditions()

Im just curious as to if its possible as I have never seen it done before.

How would the function/whatever I need to use be set out?

4
  • 1
    I don't think Python knows extension methods, at least not for builtin types. Instead, just make it a function def checkconditions(s): ... and call it as check = checkconditions(var) Commented Jun 29, 2020 at 10:58
  • It is possible, but difficult and will rely on leveraging implementation details, to dynamically add methods to built-in types, and generally it's not worth it. You can inherit from str, although often you are better off with going with collections.UserString which is easier to work with. Commented Jun 29, 2020 at 11:02
  • Note, here is one project that does this. But again, this is going to be brittle because it relies on implementation details, and so if the Python maintainers decide to change some aspect of how str is implemented, it very well could break this approach. Commented Jun 29, 2020 at 11:03
  • This is an old post from Guido van Rossum, the creator of Python, which more or less explains why the language prevents you from doing that: mail.python.org/pipermail/python-dev/2003-April/034605.html Commented Jun 29, 2020 at 11:05

3 Answers 3

2

String is a build in class/object and can not be changed. However you can make a personal new class:

class str_class:
    def __init__ (self, str):
        self.str = str

    def checkconditions(self):
        # Enter your conditions

var = str_class('hello')
check = var.checkconditions()

Or you could simply make a funtion that takes the string as input and outputs if the condition is met or not:

def checkconditions(str):
    # Enter conditions

var = 'Hello'
check = checkconditions(var)

Edit: From other comments it seems as though it is possible but not recommended.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, it can. But it probably shouldn't. But it's possible
Yeah I just read your comment on the original post, interesting, I was sure that it was not possible from having a similar issue a few weeks ago. Thank you for your input @juanpa.arrivillaga
Well, almost anything is possible if you are willing to rely on CPython implementation details and reaching into the guts of the CPython object model. But the language creators specifically prevent you from doing this in the straight-forward ways, so you would be treading off the beaten path, so to speak.
0

You can use a Class and then use the method check_conditions.

    class Check:
       def __init__(self):
            pass

        def check_conditions(string):
            #Do whatever you need in here
            print(string)

    c = Check
    c.check_conditions("hello")

This should hopefully do what you need!

Comments

0

You can't directly add the method to the original type.what you can do is subclass the type like

class mystring(str):
    def checkconditions(self):
        #condition

and then you can instantiate your new class

var = mystring('hello')
var.checkcondition()

but that's still no too practical, if you want to make it more proper you can do this

import __builtin__
__builtin__.str = mystring

var = str("hello")
check = var.checkconditions()

which achieves most of the effect desired.

Unfortunately, objects created by literal syntax will continue to be of the vanilla type and won't have your new methods/attributes.

var = 'hello'
var.checkconditions()

# Output 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'checkconditions'

"""

1 Comment

Note that The __builtin__ module was renamed to builtins in Python3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.