dry-python / dry-python/lambdas

Implements `_.method()`

Đang mở
#46 3 bình luận 1 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
286
Fork
6
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

I'm opening this issue to discuss the possible ways to implement `_.method()`

Currently, we have this implementation:
```python
class _Callable(object):
def __getattr__(
self,
key: str,
) -> Callable[[_LambdaDynamicProtocol[T1]], T1]:
return operator.attrgetter(key)
```

The problem here is that we return the _operator_ directly:
```python
>>> from lambdas import _
>>> _.method
operator.attrgetter('method')
>>> _.method()
Traceback (most recent call last):
File "", line 1, in
TypeError: attrgetter expected 1 arguments, got 0
```

---

My idea is to create an intermediated class like `_MathExpression`:
```python
class Attr: # Just ignore the name for now
def __init__(self, attr: str) -> None:
self._attrgetter = operator.attrgetter(attr)

def __call__(self, *args, **kwargs) -> Any:
if len(args) == 1:
return self._attrgetter(args[0])
return lambda obj: self._attrgetter(obj)(*args, **kwargs)
```

This implementation has a limitation, they won't work correctly if we try to call a method with one argument. This is hard because we don't know when the attribute is callable!

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.