GithubHelp home page GithubHelp logo

Comments (3)

pwwang avatar pwwang commented on May 14, 2024 1

As @alexmojaki explained, here are some detailed solutions:

  1. Use frame=2, but the class can only be subclassed once:
# with varname v0.5.6+
from varname import varname

class Item:
    def __init__(self, slots = 1):
        self.slots = slots
        self.varname = varname(frame=2)
        
class Child(Item):
    def __init__(self):
        super().__init__()
        
child = Child()
print(child.varname) # child
  1. Use __init_subclass__ to increment the frame automatically:
# with varname v0.5.6+
from varname import varname

class Item:
    FRAME = 1
    
    def __init__(self, slots = 1):
        self.slots = slots
        self.varname = varname(frame=self.__class__.FRAME)
    
    def __init_subclass__(cls):
        cls.FRAME += 1
        
class Child(Item):
    def __init__(self):
        super().__init__()
        
class Grandchild(Child):
    def __init__(self):
        super().__init__()
        
child = Child()
print(child.varname) # child

grandchild = Grandchild()
print(grandchild.varname) # grandchild
  1. with the ignore argument from v0.6.0, which will be coming soon:
# with varname v0.6.0 (coming soon)
from varname import varname

class Item:
    def __init__(self, slots = 1):
        self.slots = slots
        self.varname = varname(ignore=Child.__init__)
        
class Child(Item):
    def __init__(self):
        super().__init__()
        
child = Child()
print(child.varname) # child
  1. or to make it general, you can wrap the subclasses in a namespace or a submodule:
# with varname v0.6.0 (coming soon)
import __main__
from varname import varname

class Item:
    def __init__(self, slots = 1):
        self.slots = slots
        self.varname = varname(ignore=(__main__, 'Namespace.*.__init__'))

class Namespace:
    class Child(Item):
        def __init__(self):
            super().__init__()
            
    class Grandchild(Child):
        def __init__(self):
            super().__init__()
        
child = Namespace.Child()
print(child.varname) # child

grandchild = Namespace.Grandchild()
print(grandchild.varname) # grandchild

from python-varname.

alexmojaki avatar alexmojaki commented on May 14, 2024

Essentially Item.__init__ looks up one frame, finds super().__init__(), and can't get a varname for that.

If people only ever construct subclasses of Item and never write foo = Item() directly, you could use varname(frame=2) to skip the Child.__init__ frame, but it wouldn't work if you also made a subclass of Child.

You could write varname(ignore=[(module_containing_Child, 'Child.__init__')]) (a more convenient API is on its way in #39) but you would need to know every subclass of Item. You could keep track of that list by implementing __init_subclass__ in Item.

Or every subclass of Item could set self.varname = varname() itself and only call super().__init__() after, while Item.__init__ could check if self.varname already exists before setting it.

It's hilarious how much work has gone into ignoring frames in the upcoming #39 and still it wouldn't help with this.

from python-varname.

pwwang avatar pwwang commented on May 14, 2024

@stevenzych
v0.6.0 is there. Now you can take the solutions that work with v0.6.0.

Feel free to reopen this or open a new one if you have other questions.

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.