Comments (4)
raise_exc is (at least originally, I think) intended for suppressing a different kind of error, e.g. not being able to retrieve source code. In this case the problem is improper usage, as it's not obvious what the name of x[0]
(or subscripts in general) should be.
Similarly, you will get an error with x, y = my_func()
saying Expect a single variable on left-hand side, got 2
.
On the other hand, my_func()
without any assignment will respect raise_exc, and that's also invalid usage, so the library is not being consistent.
Seems like there should be a way to distinguish between "Something is wrong and retrieving the name is not working, you will have to find a workaround" and "This function is being used incorrectly, tell your user to assign it to a variable".
from python-varname.
Okay that makes sense thanks, though it would be good to update the docs to make that clearer as I'd expect it to be functionally equivalently to wrapping the call in try/except
, which is my current workaround.
The example I gave was improper use but I think it still represents a valid use of the library.
My use case is for writing a library that has an API somewhat similar to tensorflow and I'd like to automatically name variables in the same way e.g.
# The tensor automatically gets the name 'conv1' which can be used later by external tooling
conv1 = Conv2D(32, 3, activation='relu')
where the call to varname
is somewhere inside Conv2D
.
It would be perfectly valid for the library user to store Conv2D(...)
in a list in which case the tensor would be unnamed.
from python-varname.
@alexmojaki You guess is correct. VarnameRetrievingError
initially does not distinguish those two situations: improper use and inability to retrieve node.
We may need two different Exception classes to do so.
@GeorgeScrivener Another easy solution you could try is:
from varname.helpers import register
Conv2d = register(Conv2d)
conv1 = Conv2D(32, 3, activation='relu')
# isinstance(conv1, Conv2d)
# conv1.__varname__ == 'conv1'
Just pay attention that this solution is not thread-safe.
from python-varname.
An ImproperUseError
is added to distinguish improper use of varname()
from ast node retrieving failure, at v0.6.4.
For now, ImproperUseError
is a subclass of VarnameRetrievingError
, so that you don't have to modify your code when you try to use VarnameRetrievingError
to catch the error:
try:
x[0] = my_func()
except VarnameRetrievingError:
...
But in the future, ImproperUseError
will become independent of VarnameRetrievingError
.
Keeping this issue open until ImproperUseError
becomes independent.
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.