Creating a Class Within a Function and Access a Function Defined in the Containing Function's Scope

Creating a class within a function and access a function defined in the containing function's scope

That's an artifact of Python's name resolution rules: you only have access to the global and the local scopes, but not to the scopes in-between, e.g. not to your immediate outer scope.

EDIT: The above was poorly worded, you do have access to the variables defined in outer scopes, but by doing x = x or mymethod = mymethod from a non-global namespace, you're actually masking the outer variable with the one you're defining locally.

In example 2, your immediate outer scope is the global scope, so MyClass can see mymethod, but in example 4 your immediate outer scope is my_defining_func(), so it can't, because the outer definition of mymethod is already masked by its local definition.

See PEP 3104 for more details about nonlocal name resolution.

Also note that, for the reasons explained above, I can't get example 3 to run under either Python 2.6.5 or 3.1.2:

>>> def myfunc():
... x = 3
... class MyClass(object):
... x = x
... return MyClass
...
>>> myfunc().x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in myfunc
File "<stdin>", line 4, in MyClass
NameError: name 'x' is not defined

But the following would work:

>>> def myfunc():
... x = 3
... class MyClass(object):
... y = x
... return MyClass
...
>>> myfunc().y
3

How can I create classes with different class attributes within a function?

When parsing the class statement, the assignment to a defines it as part of the temporary class namespace, similar to an assignment to a local variable in a function definition. As such, the name a then shadows the name of the parameter in the enclosing function scope.

You can either change the parameter name (as shown by schwobaseggl)

def test_factory(a_value):
class Test:
a = a_value
return Test

or set the attribute after the definition:

def test_factory(a):
class Test:
pass
Test.a = a
return Test

or call type directly:

def test_factory(a):
return type('Test', (), {'a': a})

Why does `x = x` give an error in Python classes only when they are defined inside functions?

When you refer to the variable inside the class body, it is looked up in the global scope, regardless of where the class is defined.

The scope of the function parameter is the body of the function, not the global scope, hence the error.

>>> a = 12
>>> def f(a):
... class C:
... a = a
... print(C.a)
...
>>> f(42)
12

Why can't a function called inside another one get access to outer function's scope?

What you're talking about is called "Dynamic Scoping" and it is not possible in JavaScript. The way JavaScript and just about everything now does it is called "Lexical Scoping". You can google both of those to get a lot of discussion on the topic.

There have been languages in the past that tried out dynamic scoping. Really, though, it was determined a long time ago that dynamic scoping just makes it too hard to reason about programs and figure out what is going on.

With lexical scoping you can tell just by looking which "version" of a variable you're using, but with with dynamic scoping that might depend on who's calling you, and that makes it really hard for you to do anything using those variables.

Why is a class variable not accessible from a method?

The class body is not a nestable scope, no. The Python Execution Model explicitly excludes it:

The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods

That's because the body of a class is executed to form the class attributes; see it as a function with locals, and the locals become the attributes of the new class object.

You can then access those attributes either on the class directly (Foo.a) or via an instance (where attribute lookup falls through to the class).

Dart - How to access global scope?

you could add this line at the top of your file:

import 'dart:core' as core;

Then you would be able to refer to print as core.print(...);

The problem is that now you would have to use core. for every primitives like int, example:

core.int variable = 1;

I think this is not worth it and it's better to use another name for your print method.

How to access a variable declared on function scope from inside subscribe in angular

Overlapping function problems can prevent the value of this from disappearing by storing references to this of the parent function using the scope chain.

so using the word self, context, $this or anything else when you assign the value of this to it. it is locked in place like any other regular variable.

somethingCollection: TypeSomething[]
...
public deleteSomething(something: TypeSomething): void {
let self = this;
this.someAPI.deleteSomething(something.id).subscribe( (res) => {
self.somethingCollection // access somethingCollection....
// this.somethingCollection is undefined
}
}


Related Topics



Leave a reply



Submit