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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:01:24+00:00 2026-05-23T04:01:24+00:00

class Foo(object): pass foo = Foo() def bar(self): print ‘bar’ Foo.bar = bar foo.bar()

  • 0
class Foo(object):
  pass

foo = Foo()
def bar(self):
  print 'bar'

Foo.bar = bar
foo.bar() #bar

Coming from JavaScript, if a “class” prototype was augmented with a certain attribute. It is known that all instances of that “class” would have that attribute in its prototype chain, hence no modifications has to be done on any of its instances or “sub-classes”.

In that sense, how can a Class-based language like Python achieve Monkey patching?

  • 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-23T04:01:25+00:00Added an answer on May 23, 2026 at 4:01 am

    The real question is, how can it not? In Python, classes are first-class objects in their own right. Attribute access on instances of a class is resolved by looking up attributes on the instance, and then the class, and then the parent classes (in the method resolution order.) These lookups are all done at runtime (as is everything in Python.) If you add an attribute to a class after you create an instance, the instance will still “see” the new attribute, simply because nothing prevents it.

    In other words, it works because Python doesn’t cache attributes (unless your code does), because it doesn’t use negative caching or shadowclasses or any of the optimization techniques that would inhibit it (or, when Python implementations do, they take into account the class might change) and because everything is runtime.

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

Sidebar

Related Questions

Consider the following Python (3.x) code: class Foo(object): def bar(self): pass foo = Foo()
Can one write something like: class Test(object): def _decorator(self, foo): foo() @self._decorator def bar(self):
If I define: class Bar(object): @staticmethod def bar(): # code pass class Foo(Bar): #
Consider the following (broken) code: import functools class Foo(object): def __init__(self): def f(a,self,b): print
I have written the following class: class myClass(object): def __init__(self): pass def foo(self, arg1,
Consider this class: class foo(object): pass The default string representation looks something like this:
abstract class Foo { private List<Object> container; private bool update; Foo Foo() { container
I am using a class Foo that provides these methods: String overloadedMethod(Object) String overloadedMethod(Goo)
Is there any meaningful distinction between: class A(object): foo = 5 # some default
class Foo { static bool Bar(Stream^ stream); }; class FooWrapper { bool Bar(LPCWSTR szUnicodeString)

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.