473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

function in a function accessing vars

Hi all,

I wanted to solve a small problem, and I have a function that is
typically meant only as a function belonging inside another function.
>From the inner function I want to access a variable from the outer
function like;

def A():
some_var = 1
def B():
some_var += 1

B()
But this does not work, the function B does not recognize the
some_var. In my mind I thought the scope would propagate to the new
function and the vars would still be accessible.

How can I go about this?

With regards
- Jorgen
Jun 6 '07 #1
3 1268
Jorgen Bodde wrote:
Hi all,

I wanted to solve a small problem, and I have a function that is
typically meant only as a function belonging inside another function.
>>From the inner function I want to access a variable from the outer
function like;

def A():
some_var = 1
def B():
some_var += 1

B()
But this does not work, the function B does not recognize the
some_var. In my mind I thought the scope would propagate to the new
function and the vars would still be accessible.

How can I go about this?
The problem here is the way python determines which variables are local to a
function - by inspecting left sides.

I'm not sure if there are any fancy inspection/stackframe/cells-hacks to
accomplish what you want. But the easiest solution seems to be a
(admittedly not too beautiful)

def A():
some_var = [1]
def B(v):
v[0] += 1

B(some_var)
Or you should consider making A a callable class and thus an instance, and
some_var an instance variable. Always remember: "a closure is a poor
persons object, and an object is a poor mans closure"

Diez
Jun 6 '07 #2
Hi Diez,

Thanks, I thought it worked similar to C++ where a higher compound
could access a lower section. But as it is not straight forward, I
think it is better to embed the functionality inside a class, and make
it a member variable .. now why didn't I think of that ;-)

Thanks,
- Jorgen

On 6/6/07, Diez B. Roggisch <de***@nospam.web.dewrote:
Jorgen Bodde wrote:
Hi all,

I wanted to solve a small problem, and I have a function that is
typically meant only as a function belonging inside another function.
>From the inner function I want to access a variable from the outer
function like;

def A():
some_var = 1
def B():
some_var += 1

B()
But this does not work, the function B does not recognize the
some_var. In my mind I thought the scope would propagate to the new
function and the vars would still be accessible.

How can I go about this?

The problem here is the way python determines which variables are local to a
function - by inspecting left sides.

I'm not sure if there are any fancy inspection/stackframe/cells-hacks to
accomplish what you want. But the easiest solution seems to be a
(admittedly not too beautiful)

def A():
some_var = [1]
def B(v):
v[0] += 1

B(some_var)
Or you should consider making A a callable class and thus an instance, and
some_var an instance variable. Always remember: "a closure is a poor
persons object, and an object is a poor mans closure"

Diez
--
http://mail.python.org/mailman/listinfo/python-list
Jun 6 '07 #3
On Jun 6, 6:40 am, "Jorgen Bodde" <jorgen.maill...@gmail.comwrote:
Hi Diez,

Thanks, I thought it worked similar to C++ where a higher compound
could access a lower section.
It can 'access a lower section'; what it can't do is *change* that
'lower section'; in your example case with an int, this matters
because ints are immutable. Lists, on the other hand, are mutable. You
can *access* the methods of the list that mutate it. You're always
working with the same list, but it has different contents when you
mutate it.
But as it is not straight forward, I
think it is better to embed the functionality inside a class, and make
it a member variable .. now why didn't I think of that ;-)
Jun 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: André Somers | last post by:
Hi, I'm having a problem that I have a hard time understanding. I am getting a "Fatal error: Call to a member function on a non-object" on a constructor of a class, that looks like this...
3
by: Zim | last post by:
Greetings, I have an all purpose variable dumper that uses Var_Dump to show me data structures. I have an application wide function that I simply call to dump a structure: vardump($var);
8
by: ndsoumah | last post by:
hello guys I'm trying to get access to variables I put in a session variable from another page and it fails... here's the exact situation main file page1.php
0
by: Uwe Mayer | last post by:
Hi, I've got a class that receives a function in the constructor and uses the __call__ method to execute the aforementioned function when the instance object is called: class foo(object):...
7
by: vegetax | last post by:
I i need a decorator that adds a local variable in the function it decorates, probably related with nested scopes, for example: def dec(func): def wrapper(obj = None): if not obj : obj = Obj()...
5
by: David Rasmussen | last post by:
If I have a string that contains the name of a function, can I call it? As in: def someFunction(): print "Hello" s = "someFunction" s() # I know this is wrong, but you get the idea... ...
0
by: Marty Scholes | last post by:
I may have found a bug. I have a table: CREATE TABLE onlpcd_stat ( sel BIGSERIAL PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES onlpcd_user ON DELETE CASCADE, vars TEXT, /*...
11
by: Ryan Krauss | last post by:
I have a set of Python classes that represent elements in a structural model for vibration modeling (sort of like FEA). Some of the parameters of the model are initially unknown and I do some...
0
by: Gary Herron | last post by:
Jacob Davis wrote: Yuck, YUCK, YUCK! You are breaking *so* many good-programming-practices, I hardly know where to start. First off: A python global is not what you think. There are *no*...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.