Comments (6)
This was not a question, but rather a feature request. Also, why is the idea misguided? If the idea of automatic conversions of built-in types is so wrong, why do they exist for strings, integers, floats and all other built-in types EXCEPT decimal? Also note that the range of the type should not be a problem, because Python can use arbitrarily sized numeric types. The conversion from Python to .NET could fail with an OverflowException IMHO.
In my case I cannot even circumvent the problem by using the Equals
method. x.Decimal.Equals(42)
would work, because System.Decimal stays System.Decimal in Python. However, x.Int.Equals(42)
does not work, because System.Int32 is marshalled to Python int and therefore has no Equals method. This causes interesting surprises when working with boxed primitive types. x.Boxed.Equals
works with decimal, but fails with int.
Would be nice if guidance could be provided somehow. Thanks!
If you don't want to answer the questions here, I will be glad to open a discussion in a few days.
from pythonnet.
Generally misguided idea. You should use https://docs.python.org/3/library/decimal.html
Also not a bug. For questions please use discussions.
from pythonnet.
The particular idea is misguided because System.Decimal
does not map neither to int
- because Decimal
has fractions, nor to float
- because floats can not precisely represent decimals, and precise calculations are the whole point of decimals.
You need a compelling use case. Your last example is not very compelling either because x.Int == 42
is more idiomatic in Python, and if you want C# semantics you can use System.Object.Equals(a, b)
.
from pythonnet.
Thanks I already implemented an encoder that maps System.Decimal to decimal.Decimal, which solved most of the issues. However, I would welcome a change that adds a value parameter to the CanEncode/CanDecode methods, so the callee has all information available.
from pythonnet.
What scenario do you need this for? CanEncode
/CanDecode
are only there to quickly filter out types the codec does not support. Without them Python.NET would have to go through all codecs for every single attempt to convert anything, which would kill performance.
from pythonnet.
Sorry for taking so long to reply. I have missed the notification.
A value parameter would enable the following:
public bool CanEncode(Type type, object value)
{
return type == typeof(decimal)
|| (type == typeof(object) && Type.GetTypeCode(value?.GetType()) == TypeCode.Decimal);
}
Then it would be possible to handle boxed decimals without calling TryEncode
for all objects.
from pythonnet.
Related Issues (20)
- clr.AddReference with dot in filename, No module named error HOT 2
- Failed to use DeviceInformation class in Python program
- Failed to scan Bluetooth Low Energy (BLE) devices through Python.NET package HOT 1
- Any plan to support Android and iOS?
- Is there a way to set Python executable path explicitly. Python executable pointing to the wrong path HOT 1
- Cannot prevent cmd window from popping up when calling python script (.Net MAUI)
- Microsoft.Data.SqlClient not working.
- Allow setting typed objects via PyModule.Set HOT 3
- Loading Framework and Core based library at the same time
- Python.Runtime.PythonException:“generate_model(): incompatible function arguments. The following argument types are supported
- Does Python Installation required for this library?
- Appending library directory to PythonPath causes crash HOT 1
- Issues while trying to use Virtual Environment HOT 3
- HELP... cannot get CLR to run on Linux server: RuntimeError: Failed to create a .NET runtime (coreclr) using the parameters {}. HOT 1
- Possible causes for ModuleNotFoundError with .NET Framework DLL.
- Support for creating python sub-interpreters
- System.NotSupportedException: BinaryFormatter serialization and deserialization are disabled within this application
- pythonnet throws error while import library in conda environment
- DllNotFoundException on .net 6 with python 3.12
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 pythonnet.