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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:21:17+00:00 2026-05-26T05:21:17+00:00

if I have something like import mynewclass Can I add some method to mynewclass?

  • 0

if I have something like

import mynewclass

Can I add some method to mynewclass? Something like the following in concept:

def newmethod(self,x):
    return x + self.y

mynewclass.newmethod = newmethod

(I am using CPython 2.6)

  • 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-26T05:21:18+00:00Added an answer on May 26, 2026 at 5:21 am

    In Python the import statement is used for modules, not classes… so to import a class you need something like

    from mymodule import MyClass
    

    More to the point of your question the answer is yes. In Python classes are just regular objects and a class method is just a function stored in an object attribute.

    Attributes of object instances in Python moreover are dynamic (you can add new object attributes at runtime) and this fact, combined with the previous one means that you can add a new method to a class at runtime.

    # define a class with just one attribute
    class MyClass:
        def __init__(self, x):
            self.x = x
    
    # creates an instance of the class
    obj = MyClass(42)
    
    # now add a method to the class
    def new_method(self):
        print("x attribute is", self.x)
    MyClass.new_method = new_method
    
    # the method can be called even on already existing instances
    obj.new_method()
    

    How can this work? When you type

    obj.new_method()
    

    Python will do the following:

    1. look for new_method inside the object obj.

    2. Not finding it as an instance attribute it will try looking inside the class object (that is available as obj.__class__) where it will find the function.

    3. Now there is a bit of trickery because Python will notice that what it found is a function and therefore will "wrap" it in a closure to create what is called a "bound method". This is needed because when you call obj.new_method() you want to call MyClass.new_method(obj)… in other words binding the function to obj to create the bound method is what takes care of adding the self parameter.

    4. This bound method is what is returned by obj.new_method, and then this will be finally called because of the ending () on that line of code.

    If the search for the class also doesn’t succeed instead parent classes are also all searched in a specific order to find inherited methods and attributes and therefore things are just a little bit more complex.

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

Sidebar

Related Questions

I have something like this set up: class CategoryPage (webapp.RequestHandler): def get(self): ** DO
I have something like the following data structure: Category StartDateTime EndDateTime =============================================== 1 12/1/2009
I have control something like this #import <Foundation/Foundation.h> #import AQLevelMeter.h #import AQPlayer.h #import AQRecorder.h
Lets say i have something like this: This is file tree.py: class leaf(): def
I have something like this: # a.py import os class A: ... # b.py
Say I have something like this, which sends unhanded exceptions to logging.critical() : import
How can I create a button in Settings bundle? Something like Import SIM Contacts
I have something like this: barProgress.BeginAnimation(RangeBase.ValueProperty, new DoubleAnimation( barProgress.Value, dNextProgressValue, new Duration(TimeSpan.FromSeconds(dDuration))); Now, how
I have something like this: <node TEXT= txt A /> <node TEXT= txt X
I have something like this: <div class=content> <a href=#>A</a> </div> <div class=content> <a href=#>B</a>

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.