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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:55:17+00:00 2026-05-29T07:55:17+00:00

class A: def f(self): print(‘f’) def g(self): print(‘g’) def h(self): print(‘h’) x = A()

  • 0
class A:
  def f(self):
    print('f')
  def g(self):
    print('g')
  def h(self):
    print('h')

x = A()
y = A()
x.f = x.g # creates a new attribute 'f' for x
x.f() # 'g'; resolves at the instance attribute level to call instance method 'g'
y.f() # 'f'; instance methods are unaffected
A.f = A.h # redefines instance method 'f' to print 'h'
x.f() # 'g'; still resolves at the attribute level to call instance method 'g'
y.f() # 'h'; instance method 'f' now prints 'h'
A.g = A.h # redefines instance method 'g' to print 'h'
x.f() # 'g'; still calls the old instance method 'g' because it kept the link to it
y.f() # 'h'

Is my understanding correct?

I’m trying to use this in the following way:

  class Attributes:
    def __init__(self, params, cache_field = None):
      # ...
      self.cache_field = cache_field
      if cache_field is None:
        # I hope I'm setting instance attribute only
        self.check_cache = self.check_external_cache
      else:
        self.check_cache = self.check_internal_cache
        self.internal_cache = {}

    def check_internal_cache(self, record):
      return self.internal_cache[record.id]

    def check_external_cache(self, record):
      return record[self.cache_field]

    def calculate_attributes(self, record):
      try:
        return self.check_cache(record) # I hope it will resolve to instance attribute
      except KeyError:
        # calculate and cache the value here
        # ...

Would this work correctly? Is it ok to do this? Originally I was hoping to save time compared to checking self.cache_field in every call to calculate_attributes; but I’m no longer sure it would save any time.

  • 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-29T07:55:18+00:00Added an answer on May 29, 2026 at 7:55 am

    I think the basic idea here is correct, with a couple of minor corrections. First,

    A.f = A.h # redefines instance method 'f' to print 'h'
    

    That should read class method, not instance method. You’re changing the class here. And second, this doesn’t correspond to any defined variable here:

        if cache is None:
    

    I guess maybe you mean cache_field?

    In general, setting instance attributes in __init__ is perfectly normal and acceptable. It doesn’t matter that this is a method rather than some other kind of object — it’s not any different from saying self.foo = 'bar'.

    Also, sometimes this depends, but in general, it is indeed faster to set the method in init than to test cache_field every time check_cache is called.

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

Sidebar

Related Questions

I will go straight to the example: class Foo: @execonce def initialize(self): print 'Called'
class Ball: a = [] def __init__(self): pass def add(self,thing): self.a.append(thing) def size(self): print
Why does the following: class A(object): def __init__(self, var=[]): self._var = var print 'var
I have the following code: #!/usr/bin/ruby class Person def self.speak p = self.new puts
my code is following in python. class A(object): b = B() def d(self): print
I have a dictionary of methods: def name(self): print self.name def age(self): print self.age
class A: def x ( self ): print( self.__class__ ) class B ( A
This is what I'm trying to do in Python: class BaseClass: def __init__(self): print
Basically, what I want is to do this: class B: def fn(self): print 'B'
Assume I have some simple class class TestClass: def doSomething(self): print 'Did something' I

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.