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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:43:05+00:00 2026-06-12T09:43:05+00:00

def A(): def B(): #do something a = A() a.B() Why isn’t the above

  • 0
def A():
    def B():
        #do something

a = A()
a.B()

Why isn’t the above (such simple code) possible in Python? Is there a ‘pythonic’ (legible, unsurprising, non-hacky) workaround that does not turn A() into a class?

Edit 1: The above was explained to me that B is local to A, thus it only exists as long as A is being evaluated. So if we make it global (and be sure not to have it overriden), then why doesn’t this work?

def A():
    def B():
        #do something
    return A()

a = A()
a.B()

It says it’s returning a ‘NoneType’ object.

  • 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-12T09:43:08+00:00Added an answer on June 12, 2026 at 9:43 am

    Because a function definition just creates a name in the local namespace. What you are doing is no different than:

    def f():
        a = 2
    

    and then asking why you can’t access a from outside the function. Names bound inside a function are local to the function.

    In addition, your proposed code is strange. when you do a = f(), you are setting a to the return value of the function. Your function returns nothing, so you can’t hope to access anything through the return value. It is possible to return the inner function directly:

    def f():
       def g():
          return "blah"
       return g
    
    >>> func = f()
    >>> func()
    'blah'
    

    And this can indeed be useful. But there isn’t a generic way to access things inside the function from outside except by modifying global variables (which is usually a bad idea) or returning the values. That’s how functions work: they take inputs and return outputs; they don’t make their innards available to the outside word.

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

Sidebar

Related Questions

I know this isn't valid code, but is there a way to do something
I have this code: class SomeClass: @classmethod def func1(cls,arg1): #---Do Something--- @classmethod def func2(cls,arg1):
I have python script run.py : def do(i): # doing something with i, that
I'm trying to build a simple Tween class in javascript. The code reads something
In my Rails controller I have something like: def authenticate_user! if not current_user #
I have something like this set up: class CategoryPage (webapp.RequestHandler): def get(self): ** DO
I want to do something like this: class Dictable: def dict(self): raise NotImplementedError class
My situation is something like this: class AbstractClass: def __init__(self, property_a): self.property_a = property_a
Suppose you have something like this: class intlist: def __init__(self,l = []): self.l =
I want to do something like this... def helloWorld(): print Hello world! str.helloWorld =

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.