1

I'm not entirely certain how to word it, but I'll explain what I'm looking to do.

def function(self):
    x = "x"
    y = "y"
    z = "z"

By creating this function as a unique object of a class I should be allocating memory to the variables themselves. How, at the time of creation of the class object and calling this function, can I get the variables directly from this function?

(i.e. class.function.x)

I'm not asking for return syntax, I want direct memory mapping (or calling) of this variable without returning a value.

NationWidePants
  • 447
  • 8
  • 33
  • As far as I know you can't - that is the point of a return. If you want them to be accessed by other methods on the same class - you can make new attributes of that class : ''self.x = "x"'', but once the function/method ends then the local variables dissapear – Tony Suffolk 66 May 09 '16 at 16:29
  • @Tony Suffolk 66 The point is direct memory mapping without segment faults, not make localized variables. I need the pointers, C pointers, at least as a start. – NationWidePants May 10 '16 at 17:16
  • so use mmap - and actually directly allocate the memory. As the answers have given you - there is no direct way to access the memory of a local variable from outside the function. – Tony Suffolk 66 May 10 '16 at 17:22

3 Answers3

1

No. The scope is destroyed when the function returns, and all local variables along with it.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

I do not believe that those variables are instantiated until the function is run, and go out of scope when the functions returns. As posted I do not think what you want is possible (happy to be corrected).

However you can do,

class A(object):
    def f(self):
        pass
A.f.b = 42

when you make your class. Then later A.f.b is accessible.

James Elderfield
  • 2,389
  • 1
  • 34
  • 39
1

A basic method only remembers symbols, binding occurs upon invocation.

Python bindings 101

A class can bind variable, and so can an instance (object), and method invocation can change the instance and class variables.

http://ideone.com/KkzhRk

class Binder(object):
    x = "x"
    y = "y"
    z = "z"

    def f1(self):
        self.x = "xx"
        self.y = "yx"
        self.z = "zx"

    def f2(self):
        Binder.x = "xx"
        Binder.y = "yx"
        Binder.z = "zx"

binder = Binder()

print(Binder.x)  # class access
print(Binder.y)  # class access
print(Binder.z)  # class access
print(binder.x)  # instance access
print(binder.y)  # instance access
print(binder.z)  # instance access

binder.f1()

print(Binder.x)  # class access
print(Binder.y)  # class access
print(Binder.z)  # class access
print(binder.x)  # instance access
print(binder.y)  # instance access
print(binder.z)  # instance access

binder.f2()

print(Binder.x)  # class access
print(Binder.y)  # class access
print(Binder.z)  # class access
print(binder.x)  # instance access
print(binder.y)  # instance access
print(binder.z)  # instance access

There is also the topic of closures – nested method binding the values of the enclosing method.

http://ideone.com/bBnUJG

def mkclosure():
    x = "x"
    y = "y"
    z = "z"

    def closure():
        # Need to reference them for the closure to be created.
        return (x, y, z)

    return closure

function = mkclosure()

for cell in function.__closure__:
    print(cell.cell_contents)
totoro
  • 2,469
  • 2
  • 19
  • 23