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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:49:34+00:00 2026-05-22T20:49:34+00:00

I have a child class method I want to conditionally short circuit. What I’m

  • 0

I have a child class method I want to conditionally short circuit. What I’m trying to do is something like this except I want to put the validation logic into the base class.

class BaseClass(object):
    def getvalue(self):
        return True
    def validate(self):
        validated = self.getvalue()
        return validated

class ExtendedClass1(BaseClass):
    def do_some_work(self):
        validated = self.validate()
        if not validated:
            print "Not validated."
            return
        print "Things are validated if the method got this far.", validated

class ExtendedClass2(BaseClass):
    def do_some_work(self):
        validated = self.validate()
        if not validated:
            print "Not validated."
            return
        print "Things are validated if the method got this far.", validated

class ExtendedClass3(BaseClass):
    def do_some_work(self):
        print "This one doesn't require validation."

work1 = ExtendedClass1()
work1.do_some_work()

work2 = ExtendedClass2()
work2.do_some_work()

work3 = ExtendedClass3()
work3.do_some_work()

Following this example I was able to convert some of the repeated code into a decorator pattern.

class BaseClass(object):

    def validate(input_function):
        def wrapper(*args,**kwargs):
            validated = True
            if not validated:
                print "Not validated."
                return
            input_function(*args, **kwargs)
        return wrapper

    validate = staticmethod(validate)

class ExtendedClass1(BaseClass):
    @BaseClass.validate
    def do_some_work(self):
        print "Things are validated if the method got this far."

class ExtendedClass2(BaseClass):
    @BaseClass.validate
    def do_some_work(self):
        print "Things are validated if the method got this far."

class ExtendedClass3(BaseClass):
    def do_some_work(self):
        print "This one doesn't require validation."

work1 = ExtendedClass1()
work1.do_some_work()

work2 = ExtendedClass2()
work2.do_some_work()

work3 = ExtendedClass3()
work3.do_some_work()

However, I need to call a method of the base class in the decorator to do the validation work, and retrieve the value of (validated) in the child class. Following this example here, I modified the decorator in an attempt to let it call self.getvalue(). At this point it doesn’t error out, but it doesn’t work either because self.getvalue() doesn’t return True. This is starting to seem like more trouble than it’s worth, but now I’m curious as to whether or not it’s possible.

class BaseClass(object):

    def getvalue(self):
        return True

    def validate(self):
        def wrap(input_function):
            def wrapper(*args,**kwargs):
                validated = self.getvalue()
                if not validated:
                    print "Not validated."
                    return
                input_function(*args, **kwargs)
            return wrapper
        return wrap

    validate = staticmethod(validate)

class ExtendedClass1(BaseClass):
    @BaseClass.validate
    def do_some_work(self):
        print "Things are validated if the method got this far."#, validated

class ExtendedClass2(BaseClass):
    @BaseClass.validate
    def do_some_work(self):
        print "Things are validated if the method got this far."#, validated

class ExtendedClass3(BaseClass):
    def do_some_work(self):
        print "This one doesn't require validation."

work1 = ExtendedClass1()
work1.do_some_work()

work2 = ExtendedClass2()
work2.do_some_work()

work3 = ExtendedClass3()
work3.do_some_work()

Is it possible to set an attribute with the decorator and then retrieve it later?

                ...
                self.validated = True
                if not self.validated:
                    print "Not validated."
                    return
                ...
print work1.validated
                ...

AttributeError: 'ExtendedClass1' object has no attribute 'validated'

Essentially, I want to turn this:

class ExtendedClass1(BaseClass):
    def do_some_work(self):
        validated = self.validate()
        if not validated:
            print "Not validated."
            return
        print "Things are validated if the method got this far.", validated

Into this:

class ExtendedClass1(BaseClass):
    @BaseClass.validate
    def do_some_work(self):
        print "Things are validated if the method got this far.", validated

Using the suggestion posted by Zaur Nasibov, this example satisfies my use case. I’m still interested to know if @validate can be implemented as a method instead of a stand alone function, but this gets the job done.

class BaseClass(object):
    def getvalue(self):
        return True

def validate(func):
    def wrapped(self, *args, **kwargs):
        validated = self.getvalue()
        self.validated = validated
        if not validated:
            print "Not validated."
            return
        func(self, *args, **kwargs)
    return wrapped

class ExtendedClass1(BaseClass):
    @validate
    def do_some_work(self,input):
        print "Things are validated if the method got this far.", self.validated, input

class ExtendedClass2(BaseClass):
    @validate
    def do_some_work(self):
        print "Things are validated if the method got this far.", self.validated

class ExtendedClass3(BaseClass):
    def do_some_work(self):
        print "This one doesn't require validation."#, self.validated

work1 = ExtendedClass1()
work1.do_some_work(input="some text")

work2 = ExtendedClass2()
work2.do_some_work()

work3 = ExtendedClass3()
work3.do_some_work()
  • 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-22T20:49:35+00:00Added an answer on May 22, 2026 at 8:49 pm

    @tponthieux, what you can do is setting the attribute of the called function (method) and then retrieving it:

    Simple example (updated):

    def validate(func):    
        def wrapped(self, *args, **kwargs):
            self.valid = True
            func(self, *args, **kwargs)
        return wrapped
    
    class TestClass(object):
        @validate
        def do_some_work(self):
            print "some work done"
    
    tc = TestClass()
    tc.do_some_work()
    print tc.valid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a parent-child that defines some method .foo() like this: class Parent
I have a method and two classes defined like this: template<template<class X> class T>
I have a problem. It looks like ARC synchronizes my property with child class.
I have a static method defined in a base class, I want to override
I have 2 classes, Parent and Child, and Parent has a class method named
I have a child class of AuthorizeAttribute named CheckArticleExistence. I would like to set
Lets say I have this class class Child { public string FirstName { get;
In objective-C I want to have a child class call or invoke a parent's
I've defined a class, and I'd like to have a class method (or variable,
I have code that looks like this: Class Parent: def someMethod(self): return 42 Class

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.