I
I
iegor2016-05-23 12:14:47
Python
iegor, 2016-05-23 12:14:47

How to access scope?

There are two functions: one is called by the second, is it possible to get access to the variables of the calling function from the called one?

def a():
    ### хочу получить x из функции b

def b(x):
    return a()

it is necessary to edit exactly a, b such as it is.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor Stakhovsky, 2016-05-23
@iegor

import sys


def a():
  j = 10
  h = 20
  b()


def b():
  callingframe = sys._getframe(1)
  print(callingframe.f_locals)

a()

A
Alexander, 2016-05-23
@Avernial

The locals function returns a dictionary with variables in the current scope.

def func1(a, b):
    func2(locals())

def func2(args):
    print(args)

func1(10, 20)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question