Comments (3)
As @alexmojaki explained, here are some detailed solutions:
- 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
- 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
- with the
ignore
argument fromv0.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
- 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.
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.
@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)
- How to get argname of chain function HOT 6
- Is there a way to get the argname with exec or eval HOT 4
- Is there a way we can set the Subscription for varname? HOT 2
- argname sample code raises VarnameRetrievingError HOT 1
- VarnameRetrievingError: Cannot retrieve the node where the function is called.
- Error retrieving value for `__setitem__` HOT 5
- nameof error when running program, but not when running in debug HOT 13
- argname seems not working HOT 2
- Performance issues HOT 2
- Obtain current scope and pass into function HOT 2
- VarnameRetrievingError when I use function as part of module HOT 10
- how to nameof inside a loop ? HOT 1
- how to get arg name in sub function? HOT 10
- argname doesn't seem to function, "cannot retrieve the node where the function is called" HOT 7
- How to print the variable names inside a for loop ? HOT 8
- some cases of argname don't know how to deal HOT 2
- Partial assignment in case a starred variable is present HOT 6
- "VarnameRetrievingError: Unable to retrieve the ast node." / "VarnameRetrievingError: Cannot retrieve the node where the function is called." HOT 1
- Traversal list HOT 1
- "Unable to retrieve the ast node" issue HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-varname.