Giter VIP home page Giter VIP logo

Comments (2)

pwwang avatar pwwang commented on May 22, 2024

That's something like super(), short for super(SomeClass, self).
This video has a rough implementation for it: https://www.youtube.com/watch?v=X1PQ7zzltz4
The idea is similar to the "vanilla solution" you mentioned, which uses current_frame.f_back.f_locals to trace the local variables from the previous frame.

The core functions from varname handle the variable names that are explicitly given. The implementation behind the scenes is similar to the "vanilla solution". However, the variable/arguments are hidden in your case, which is out of the scope of this package.

The function you wanted can be implemented with a very simple wrapper around that "vanilla solution":

import sys                                      
                                                
def get_prev_local(name):          
  # frame 1: inside fun2
  # frame 2: inside fun1             
  return sys._getframe(2).f_locals[name]        
                                                
def fun1():                                     
  self = 1                                      
  fun2()                                        
                                                
def fun2():                                     
  self = get_prev_local("self")                 
  print(self)                                   
                                                
fun1()    # prints 1                                      

from python-varname.

pwwang avatar pwwang commented on May 22, 2024

Close it for now. Feel free to reopen.

from python-varname.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.