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

  • Home
  • SEARCH
  • 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 8540061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:30:52+00:00 2026-06-11T11:30:52+00:00

Say I have a class and a function: class AddressValidator(self): def __init__(self): pass def

  • 0

Say I have a class and a function:

class AddressValidator(self):
    def __init__(self):
        pass
    def validate(address):
        # ...

def validate_address(addr):
    validator = AddressValidator()
    return validator.validate(addr)

The function is a shortcut for using the class, if you will. Now, what if this function has to be run thousands of times? If the validator class actually has to do something on instantiation, like connecting to a database, creating it over and over thousands of times is pretty wasteful. I was wondering if I could perhaps do something like this:

def validate_address(addr):
    if not hasattr(validate_address, 'validator'):
        validate_address.validator = AddressValidator()

    validator = validate_address.validator
    return validator.validate(addr)

Now the validator class is only instantiated once and saved “in the function”, to put it that way. I’ve never seen this done though, so I’m guessing it’s bad practice. If so, why?

Note: I know I can just cache the validator object in a module global. I’m just curious if this is a viable solution when I want to avoid littering my module.

  • 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-11T11:30:53+00:00Added an answer on June 11, 2026 at 11:30 am

    Despite “everithing is an object”, not everithing work as nice as instances of well controlled class.
    This problem looks like typical case for “functor” or “callable object” as it called in python.

    the code will be look something like

    class AddressValidator(self):
        def __init__(self):
            pass
        def __call__(self,address):
            # ...
    
    validate_address = AdressValidator()
    

    or you could just define your function as shortcut to bound method

    class AddressValidator(self):
        def __init__(self):
            pass
        def validate(self,address):
            # ...
    
    validate_adress = AdressValidator().validate
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have this : class whatever(object): def __init__(self): pass and this function:
Let's say I have a class class Foo: def __init__(self): pass def meth1(self, *args):
Lets say i have the following code. class A { function one() { return
Lets say I have: class X { function a() { echo Hello, ; }
say I have this class: class animal { function noise() { print 'woof'; }
Say I have a type with a member function: class Thing { std::string m_name;
class Manage { spl_autoload_register(function($class) { include $class . '.class.php'; }); } Say I have
Say I have : class Foo { public: Foo(std::function< void(std::shared_ptr< Foo >) > callback);
Lets say I have a class like this in Java: public class Function {
Say I have a class like this: class Person { private $value; public function

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.