Skip to content

Commit 283d79d

Browse files
committed
[Python] Avoid need for __len__ pythonizations by implementing size()
We prefer C++ code that get pythonized automatically by the heuristics in `cppyy`. One of these heuristics is the automatic implementation of `__len__` in terms of the `size()` method on the C++ side.
1 parent 0f34653 commit 283d79d

File tree

9 files changed

+8
-16
lines changed

9 files changed

+8
-16
lines changed

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tarray.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ def pythonize_tarray(klass, name):
4848
# klass: class to be pythonized
4949
# name: string containing the name of the class
5050

51-
if name == 'TArray':
52-
# Support `len(a)` as `a.GetSize()`
53-
klass.__len__ = klass.GetSize
54-
else:
51+
if not name == 'TArray':
5552
# Add checked __getitem__. It has to be directly added to the TArray
5653
# subclasses, which have a default __getitem__.
5754
# The new __getitem__ allows to throw pythonic IndexError when index

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tcollection.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ def pythonize_tcollection(klass):
9898
# Parameters:
9999
# klass: class to be pythonized
100100

101-
# Support `len(c)` as `c.GetEntries()`
102-
klass.__len__ = klass.GetEntries
103-
104101
# Add Python lists methods
105102
klass.append = klass.Add
106103
klass.remove = _remove_pyz

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tstring.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ def pythonize_tstring(klass):
1616
# Parameters:
1717
# klass: class to be pythonized
1818

19-
# Support `len(s)` as `s.Length()`
20-
klass.__len__ = klass.Length
21-
2219
# Add string representation
2320
klass.__str__ = klass.Data
2421
klass.__repr__ = lambda self: "'{}'".format(self)

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tvector3.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ def pythonize_tvector3(klass):
1818
# Parameters:
1919
# klass: class to be pythonized
2020

21-
# `len(v)` is always 3
22-
klass.__len__ = lambda _: 3
23-
2421
# Add checked __getitem__.
2522
# Allows to throw pythonic IndexError when index is out of range
2623
# and to iterate over the vector.

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tvectort.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ def pythonize_tvectort(klass):
1818
# Parameters:
1919
# klass: class to be pythonized
2020

21-
# Support `len(v)` as `v.GetNoElements()`
22-
klass.__len__ = klass.GetNoElements
23-
2421
# Add checked __getitem__.
2522
# Allows to throw pythonic IndexError when index is out of range
2623
# and to iterate over the vector.

core/base/inc/TString.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ friend std::strong_ordering operator<=>(const TString &s1, const TString &s2) {
423423
Bool_t IsWhitespace() const { return (Length() == CountChar(' ')); }
424424
Ssiz_t Last(char c) const;
425425
Ssiz_t Length() const { return IsLong() ? GetLongSize() : GetShortSize(); }
426+
inline std::size_t size() const { return Length(); }
426427
Bool_t MaybeRegexp() const;
427428
Bool_t MaybeWildcard() const;
428429
TString MD5() const;

core/cont/inc/TArray.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class TArray {
4747
Int_t GetSize() const { return fN; }
4848
virtual void Set(Int_t n) = 0;
4949

50+
inline std::size_t size() const { return GetSize(); }
51+
5052
virtual Double_t GetAt(Int_t i) const = 0;
5153
virtual void SetAt(Double_t v, Int_t i) = 0;
5254

core/cont/inc/TCollection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class TCollection : public TObject {
177177
TObject *operator()(const char *name) const;
178178
TObject *FindObject(const TObject *obj) const override;
179179
virtual Int_t GetEntries() const { return GetSize(); }
180+
inline std::size_t size() const { return GetEntries(); }
180181
const char *GetName() const override;
181182
virtual TObject **GetObjectRef(const TObject *obj) const = 0;
182183
/// Return the *capacity* of the collection, i.e. the current total amount of space that has been allocated so far.

math/physics/inc/TVector3.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class TVector3 : public TObject {
4040
~TVector3() override = default;
4141
// Destructor
4242

43+
/// The length is always 3. For compatibility with the standard library.
44+
constexpr std::size_t size() const { return 3; }
45+
4346
Double_t operator () (int) const;
4447
inline Double_t operator [] (int) const;
4548
// Get components by index (Geant4).

0 commit comments

Comments
 (0)