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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:20:24+00:00 2026-06-18T22:20:24+00:00

I have a code in which I want to defina a class function inside

  • 0

I have a code in which I want to defina a class function inside a class function. Here’s a simpler example of what I want to do. The goal of this program is to print 4.

>>> class bluh:
...     def haha(self):
...             print 3
...     def __init__(self):
...             def haha(self):
...                     print 4
... 
>>> x = bluh()
>>> x.haha()
3

How should I actually write this program to do what I want?

  • 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-18T22:20:25+00:00Added an answer on June 18, 2026 at 10:20 pm

    This really depends on what you want to do.

    >>> class Foo(object):
    ...     def haha(self):
    ...         print 3
    ...     def __init__(self):
    ...         def haha():
    ...             print 4
    ...         self.haha = haha
    ... 
    >>> a = Foo()
    >>> a.haha
    <function haha at 0x7f4539e25aa0>
    >>> a.haha()
    4
    

    In the previous example, haha isn’t actually a method — It’s just a function. But it will pick up a reference to self from the closure and a lot of the time, that’s probably good enough. If you actually want to monkeypatch/duck punch a new method on an instance, you’ll need to use types.MethodType. See here for an example.

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

Sidebar

Related Questions

i have this javascript code to which i want to add the jquery fade-in
I have code, which i want to run, this takes a few mins so
So i have this code which makes a box, but want to make the
I have some code which use 'this' pointer of class which calls this code.
I have some code which I want to document with in-body comments like so:
I have the code in which i want to find out the greatest number
I have the following code: // Dictionary which I want to optimize Dictionary<string, MyClass>
I have the following php code which I want to add a delay too:
I have a javascript code using which I want to accomplish the following .
I have written a code in which i want to open a HTM file

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.