0
# Objects Module
1
2
The Objects module provides 49 functions for manipulating objects and dictionaries. These functions cover property access, merging, cloning, transformation, and type conversion operations.
3
4
## Property Access Functions
5
6
### get
7
8
```python { .api }
9
def get(obj: Any, path: Union[str, List], default: Any = None) -> Any
10
```
11
12
Gets the value at path of object. If the resolved value is `None` or doesn't exist, the `default` value is returned in its place.
13
14
**Parameters:**
15
- `obj` (`Any`): Object to query.
16
- `path` (`Union[str, List]`): Path of the property to get.
17
- `default` (`Any`): Value returned for `None` resolved values. Defaults to `None`.
18
19
**Returns:**
20
- `Any`: Resolved value.
21
22
**Example:**
23
```python { .api }
24
from pydash import get
25
26
obj = {'a': [{'b': {'c': 3}}]}
27
get(obj, 'a[0].b.c')
28
# 3
29
30
get(obj, 'a.b.c', 'default')
31
# 'default'
32
33
get(obj, ['a', 0, 'b', 'c'])
34
# 3
35
```
36
37
### has
38
39
```python { .api }
40
def has(obj: Any, path: Union[str, List]) -> bool
41
```
42
43
Checks if `path` is a direct property of `object`.
44
45
**Parameters:**
46
- `obj` (`Any`): Object to query.
47
- `path` (`Union[str, List]`): Path to check.
48
49
**Returns:**
50
- `bool`: `True` if `path` exists, else `False`.
51
52
**Example:**
53
```python { .api }
54
from pydash import has
55
56
obj = {'a': {'b': 2}}
57
has(obj, 'a')
58
# True
59
60
has(obj, 'a.b')
61
# True
62
63
has(obj, 'c')
64
# False
65
```
66
67
### set_
68
69
```python { .api }
70
def set_(obj: T, path: Union[str, List], value: Any) -> T
71
```
72
73
Sets the value at `path` of `object`. If a portion of `path` doesn't exist, it's created. Arrays are created for missing index properties while objects are created for all other missing properties.
74
75
**Parameters:**
76
- `obj` (`T`): Object to modify.
77
- `path` (`Union[str, List]`): Path of the property to set.
78
- `value` (`Any`): Value to set.
79
80
**Returns:**
81
- `T`: `obj`.
82
83
**Example:**
84
```python { .api }
85
from pydash import set_
86
87
obj = {'a': [{'b': {'c': 3}}]}
88
set_(obj, 'a[0].b.c', 4)
89
# obj is now {'a': [{'b': {'c': 4}}]}
90
91
set_(obj, 'x[0].y.z', 5)
92
# obj is now {'a': [{'b': {'c': 4}}], 'x': [{'y': {'z': 5}}]}
93
```
94
95
### set_with
96
97
```python { .api }
98
def set_with(obj: Any, path: Union[str, List], value: Any, customizer: Callable = None) -> Any
99
```
100
101
Like `set_` except that it accepts `customizer` which is invoked to produce the objects of `path`.
102
103
**Parameters:**
104
- `obj` (`Any`): Object to modify.
105
- `path` (`Union[str, List]`): Path of the property to set.
106
- `value` (`Any`): Value to set.
107
- `customizer` (`Callable`, optional): Function to customize assigned values.
108
109
**Returns:**
110
- `Any`: `obj`.
111
112
### unset
113
114
```python { .api }
115
def unset(obj: Union[List, Dict], path: Union[str, List]) -> bool
116
```
117
118
Removes the property at `path` of `object`.
119
120
**Parameters:**
121
- `obj` (`Union[List, Dict]`): Object to modify.
122
- `path` (`Union[str, List]`): Path of the property to unset.
123
124
**Returns:**
125
- `bool`: `True` if the property is deleted, else `False`.
126
127
**Example:**
128
```python { .api }
129
from pydash import unset
130
131
obj = {'a': [{'b': {'c': 7}}]}
132
unset(obj, 'a[0].b.c')
133
# True
134
# obj is now {'a': [{'b': {}}]}
135
```
136
137
### update
138
139
```python { .api }
140
def update(obj: Any, path: Union[str, List], updater: Callable) -> Any
141
```
142
143
This method is like `set_` except that it accepts `updater` to produce the value to set.
144
145
**Parameters:**
146
- `obj` (`Any`): Object to modify.
147
- `path` (`Union[str, List]`): Path of the property to set.
148
- `updater` (`Callable`): Function to produce the updated value.
149
150
**Returns:**
151
- `Any`: `obj`.
152
153
**Example:**
154
```python { .api }
155
from pydash import update
156
157
obj = {'a': [{'b': {'c': 3}}]}
158
update(obj, 'a[0].b.c', lambda x: x * 2)
159
# obj is now {'a': [{'b': {'c': 6}}]}
160
```
161
162
### update_with
163
164
```python { .api }
165
def update_with(obj: Any, path: Union[str, List], updater: Callable, customizer: Callable = None) -> Any
166
```
167
168
Like `update` except that it accepts `customizer` which is invoked to produce the objects of `path`.
169
170
**Parameters:**
171
- `obj` (`Any`): Object to modify.
172
- `path` (`Union[str, List]`): Path of the property to set.
173
- `updater` (`Callable`): Function to produce the updated value.
174
- `customizer` (`Callable`, optional): Function to customize assigned values.
175
176
**Returns:**
177
- `Any`: `obj`.
178
179
## Key/Value Operations
180
181
### keys
182
183
```python { .api }
184
def keys(obj: Any) -> List[Any]
185
```
186
187
Creates a list of the own enumerable property names of `object`.
188
189
**Parameters:**
190
- `obj` (`Any`): Object to query.
191
192
**Returns:**
193
- `List[Any]`: List of property names.
194
195
**Example:**
196
```python { .api }
197
from pydash import keys
198
199
keys({'a': 1, 'b': 2, 'c': 3})
200
# ['a', 'b', 'c']
201
202
keys([1, 2, 3])
203
# [0, 1, 2]
204
```
205
206
### values
207
208
```python { .api }
209
def values(obj: Any) -> List[Any]
210
```
211
212
Creates a list of the own enumerable property values of `object`.
213
214
**Parameters:**
215
- `obj` (`Any`): Object to query.
216
217
**Returns:**
218
- `List[Any]`: List of property values.
219
220
**Example:**
221
```python { .api }
222
from pydash import values
223
224
values({'a': 1, 'b': 2, 'c': 3})
225
# [1, 2, 3]
226
227
values([1, 2, 3])
228
# [1, 2, 3]
229
```
230
231
### to_pairs
232
233
```python { .api }
234
def to_pairs(obj: Any) -> List[Tuple[Any, Any]]
235
```
236
237
Creates a list of key-value pairs from `object`.
238
239
**Parameters:**
240
- `obj` (`Any`): Object to query.
241
242
**Returns:**
243
- `List[Tuple[Any, Any]]`: List of key-value pairs.
244
245
**Example:**
246
```python { .api }
247
from pydash import to_pairs
248
249
to_pairs({'a': 1, 'b': 2})
250
# [('a', 1), ('b', 2)]
251
```
252
253
### invert
254
255
```python { .api }
256
def invert(obj: Union[Mapping, Iterable]) -> Dict[Any, Any]
257
```
258
259
Creates an object composed of the inverted keys and values of `object`. If `object` contains duplicate values, subsequent values overwrite property assignments of previous values.
260
261
**Parameters:**
262
- `obj` (`Union[Mapping, Iterable]`): Object to invert.
263
264
**Returns:**
265
- `Dict[Any, Any]`: New inverted object.
266
267
**Example:**
268
```python { .api }
269
from pydash import invert
270
271
invert({'a': 1, 'b': 2, 'c': 1})
272
# {1: 'c', 2: 'b'}
273
274
invert(['a', 'b', 'c'])
275
# {'a': 0, 'b': 1, 'c': 2}
276
```
277
278
### invert_by
279
280
```python { .api }
281
def invert_by(obj: Mapping, iteratee: Callable = None) -> Dict[Any, List[Any]]
282
```
283
284
Like `invert` except that the inverted object is generated from the results of running each element of `object` through `iteratee`. The corresponding inverted value of each inverted key is a list of keys responsible for generating the inverted value.
285
286
**Parameters:**
287
- `obj` (`Mapping`): Object to invert.
288
- `iteratee` (`Callable`, optional): Function invoked per element.
289
290
**Returns:**
291
- `Dict[Any, List[Any]]`: New inverted object.
292
293
**Example:**
294
```python { .api }
295
from pydash import invert_by
296
297
invert_by({'a': 1, 'b': 2, 'c': 1})
298
# {1: ['a', 'c'], 2: ['b']}
299
```
300
301
### map_keys
302
303
```python { .api }
304
def map_keys(obj: Any, iteratee: Callable = None) -> Dict[Any, Any]
305
```
306
307
Creates an object with the same values as `object` and keys generated by running each own enumerable property of `object` through `iteratee`.
308
309
**Parameters:**
310
- `obj` (`Any`): Object to iterate over.
311
- `iteratee` (`Callable`, optional): Function invoked per iteration.
312
313
**Returns:**
314
- `Dict[Any, Any]`: New mapped object.
315
316
**Example:**
317
```python { .api }
318
from pydash import map_keys
319
320
map_keys({'a': 1, 'b': 2}, lambda val, key: key + val)
321
# {'a1': 1, 'b2': 2}
322
```
323
324
### map_values
325
326
```python { .api }
327
def map_values(obj: Any, iteratee: Callable = None) -> Dict[Any, Any]
328
```
329
330
Creates an object with the same keys as `object` and values generated by running each own enumerable property of `object` through `iteratee`.
331
332
**Parameters:**
333
- `obj` (`Any`): Object to iterate over.
334
- `iteratee` (`Callable`, optional): Function invoked per iteration.
335
336
**Returns:**
337
- `Dict[Any, Any]`: New mapped object.
338
339
**Example:**
340
```python { .api }
341
from pydash import map_values
342
343
map_values({'a': 1, 'b': 2}, lambda x: x * 2)
344
# {'a': 2, 'b': 4}
345
346
# Using property shorthand
347
users = {'john': {'age': 30}, 'jane': {'age': 25}}
348
map_values(users, 'age')
349
# {'john': 30, 'jane': 25}
350
```
351
352
### map_values_deep
353
354
```python { .api }
355
def map_values_deep(obj: Any, iteratee: Callable = None) -> Any
356
```
357
358
Like `map_values` but recursively maps nested values.
359
360
**Parameters:**
361
- `obj` (`Any`): Object to iterate over.
362
- `iteratee` (`Callable`, optional): Function invoked per iteration.
363
364
**Returns:**
365
- `Any`: New mapped object.
366
367
**Example:**
368
```python { .api }
369
from pydash import map_values_deep
370
371
obj = {'a': {'b': {'c': 1}}, 'd': [{'e': 2}]}
372
map_values_deep(obj, lambda x: x * 2 if isinstance(x, int) else x)
373
# {'a': {'b': {'c': 2}}, 'd': [{'e': 4}]}
374
```
375
376
## Object Filtering and Selection
377
378
### omit
379
380
```python { .api }
381
def omit(obj: Any, *properties: Union[str, List]) -> Dict[Any, Any]
382
```
383
384
Creates an object composed of the own and inherited enumerable properties of `object` that are not omitted.
385
386
**Parameters:**
387
- `obj` (`Any`): Source object.
388
- `properties` (`*Union[str, List]`): Properties to omit.
389
390
**Returns:**
391
- `Dict[Any, Any]`: New object with omitted properties.
392
393
**Example:**
394
```python { .api }
395
from pydash import omit
396
397
omit({'a': 1, 'b': 2, 'c': 3}, 'a', 'c')
398
# {'b': 2}
399
400
omit({'a': {'b': 2}, 'c': 3}, 'a.b')
401
# {'a': {}, 'c': 3}
402
```
403
404
### omit_by
405
406
```python { .api }
407
def omit_by(obj: Any, iteratee: Callable = None) -> Dict[Any, Any]
408
```
409
410
Creates an object composed of the own and inherited enumerable properties of `object` that `iteratee` doesn't return truthy for.
411
412
**Parameters:**
413
- `obj` (`Any`): Source object.
414
- `iteratee` (`Callable`, optional): Function invoked per property.
415
416
**Returns:**
417
- `Dict[Any, Any]`: New object with omitted properties.
418
419
**Example:**
420
```python { .api }
421
from pydash import omit_by
422
423
omit_by({'a': 1, 'b': '2', 'c': 3}, lambda x: isinstance(x, str))
424
# {'a': 1, 'c': 3}
425
```
426
427
### pick
428
429
```python { .api }
430
def pick(obj: Any, *properties: Union[str, List]) -> Dict[Any, Any]
431
```
432
433
Creates an object composed of the picked `object` properties.
434
435
**Parameters:**
436
- `obj` (`Any`): Source object.
437
- `properties` (`*Union[str, List]`): Properties to pick.
438
439
**Returns:**
440
- `Dict[Any, Any]`: New object with picked properties.
441
442
**Example:**
443
```python { .api }
444
from pydash import pick
445
446
pick({'a': 1, 'b': 2, 'c': 3}, 'a', 'c')
447
# {'a': 1, 'c': 3}
448
449
pick({'a': {'b': 2}, 'c': 3}, 'a.b')
450
# {'a': {'b': 2}}
451
```
452
453
### pick_by
454
455
```python { .api }
456
def pick_by(obj: Any, iteratee: Callable = None) -> Dict[Any, Any]
457
```
458
459
Creates an object composed of the own and inherited enumerable properties of `object` that `iteratee` returns truthy for.
460
461
**Parameters:**
462
- `obj` (`Any`): Source object.
463
- `iteratee` (`Callable`, optional): Function invoked per property.
464
465
**Returns:**
466
- `Dict[Any, Any]`: New object with picked properties.
467
468
**Example:**
469
```python { .api }
470
from pydash import pick_by
471
472
pick_by({'a': 1, 'b': '2', 'c': 3}, lambda x: isinstance(x, int))
473
# {'a': 1, 'c': 3}
474
```
475
476
### find_key
477
478
```python { .api }
479
def find_key(obj: Any, predicate: Callable = None) -> Any
480
```
481
482
Returns the key of the first element `predicate` returns truthy for.
483
484
**Parameters:**
485
- `obj` (`Any`): Object to inspect.
486
- `predicate` (`Callable`, optional): Function invoked per iteration.
487
488
**Returns:**
489
- `Any`: Key of matched element, else `None`.
490
491
**Example:**
492
```python { .api }
493
from pydash import find_key
494
495
users = {
496
'barney': {'age': 36, 'active': True},
497
'fred': {'age': 40, 'active': False},
498
'pebbles': {'age': 1, 'active': True}
499
}
500
find_key(users, lambda x: x['age'] < 40)
501
# 'barney'
502
```
503
504
### find_last_key
505
506
```python { .api }
507
def find_last_key(obj: Any, predicate: Callable = None) -> Any
508
```
509
510
Like `find_key` except that it iterates over elements in the opposite order.
511
512
**Parameters:**
513
- `obj` (`Any`): Object to inspect.
514
- `predicate` (`Callable`, optional): Function invoked per iteration.
515
516
**Returns:**
517
- `Any`: Key of matched element, else `None`.
518
519
## Object Merging and Assignment
520
521
### assign
522
523
```python { .api }
524
def assign(obj: Any, *sources: Any) -> Any
525
```
526
527
Assigns own enumerable string keyed properties of source objects to the destination object. Source objects are applied from left to right.
528
529
**Parameters:**
530
- `obj` (`Any`): Destination object.
531
- `sources` (`*Any`): Source objects.
532
533
**Returns:**
534
- `Any`: `obj`.
535
536
**Example:**
537
```python { .api }
538
from pydash import assign
539
540
assign({'a': 1}, {'b': 2}, {'c': 3})
541
# {'a': 1, 'b': 2, 'c': 3}
542
```
543
544
### assign_with
545
546
```python { .api }
547
def assign_with(obj: Any, *sources: Any, customizer: Callable = None) -> Any
548
```
549
550
Like `assign` except that it accepts `customizer` which is invoked to produce the assigned values.
551
552
**Parameters:**
553
- `obj` (`Any`): Destination object.
554
- `sources` (`*Any`): Source objects.
555
- `customizer` (`Callable`, optional): Function to customize assigned values.
556
557
**Returns:**
558
- `Any`: `obj`.
559
560
### defaults
561
562
```python { .api }
563
def defaults(obj: Any, *sources: Any) -> Any
564
```
565
566
Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `None` or are not defined.
567
568
**Parameters:**
569
- `obj` (`Any`): Destination object.
570
- `sources` (`*Any`): Source objects.
571
572
**Returns:**
573
- `Any`: `obj`.
574
575
**Example:**
576
```python { .api }
577
from pydash import defaults
578
579
defaults({'a': 1}, {'b': 2}, {'a': 3, 'c': 3})
580
# {'a': 1, 'b': 2, 'c': 3}
581
```
582
583
### defaults_deep
584
585
```python { .api }
586
def defaults_deep(obj: Any, *sources: Any) -> Any
587
```
588
589
Like `defaults` except that it recursively assigns default properties.
590
591
**Parameters:**
592
- `obj` (`Any`): Destination object.
593
- `sources` (`*Any`): Source objects.
594
595
**Returns:**
596
- `Any`: `obj`.
597
598
**Example:**
599
```python { .api }
600
from pydash import defaults_deep
601
602
defaults_deep({'a': {'b': 2}}, {'a': {'b': 1, 'c': 3}})
603
# {'a': {'b': 2, 'c': 3}}
604
```
605
606
### merge
607
608
```python { .api }
609
def merge(obj: Any, *sources: Any) -> Any
610
```
611
612
Recursively merges own and inherited enumerable string keyed properties of source objects into the destination object.
613
614
**Parameters:**
615
- `obj` (`Any`): Destination object.
616
- `sources` (`*Any`): Source objects.
617
618
**Returns:**
619
- `Any`: `obj`.
620
621
**Example:**
622
```python { .api }
623
from pydash import merge
624
625
obj = {'a': [{'b': 2}, {'d': 4}]}
626
other = {'a': [{'c': 3}, {'e': 5}]}
627
merge(obj, other)
628
# {'a': [{'b': 2, 'c': 3}, {'d': 4, 'e': 5}]}
629
```
630
631
### merge_with
632
633
```python { .api }
634
def merge_with(obj: Any, *sources: Any, customizer: Callable = None) -> Any
635
```
636
637
Like `merge` except that it accepts `customizer` which is invoked to produce the merged values.
638
639
**Parameters:**
640
- `obj` (`Any`): Destination object.
641
- `sources` (`*Any`): Source objects.
642
- `customizer` (`Callable`, optional): Function to customize assigned values.
643
644
**Returns:**
645
- `Any`: `obj`.
646
647
## Object Cloning
648
649
### clone
650
651
```python { .api }
652
def clone(value: T) -> T
653
```
654
655
Creates a shallow clone of `value`.
656
657
**Parameters:**
658
- `value` (`T`): Value to clone.
659
660
**Returns:**
661
- `T`: Cloned value.
662
663
**Example:**
664
```python { .api }
665
from pydash import clone
666
667
objects = [{'a': 1}, {'b': 2}]
668
shallow = clone(objects)
669
# shallow == objects but shallow is not objects
670
# shallow[0] is objects[0] (shallow clone)
671
```
672
673
### clone_deep
674
675
```python { .api }
676
def clone_deep(value: T) -> T
677
```
678
679
Creates a deep clone of `value`.
680
681
**Parameters:**
682
- `value` (`T`): Value to clone.
683
684
**Returns:**
685
- `T`: Deep cloned value.
686
687
**Example:**
688
```python { .api }
689
from pydash import clone_deep
690
691
objects = [{'a': 1}, {'b': 2}]
692
deep = clone_deep(objects)
693
# deep == objects but deep is not objects
694
# deep[0] is not objects[0] (deep clone)
695
```
696
697
### clone_with
698
699
```python { .api }
700
def clone_with(value: Any, customizer: Callable = None) -> Any
701
```
702
703
Like `clone` except that it accepts `customizer` which is invoked to produce the cloned value.
704
705
**Parameters:**
706
- `value` (`Any`): Value to clone.
707
- `customizer` (`Callable`, optional): Function to customize cloning.
708
709
**Returns:**
710
- `Any`: Cloned value.
711
712
### clone_deep_with
713
714
```python { .api }
715
def clone_deep_with(value: Any, customizer: Callable = None) -> Any
716
```
717
718
Like `clone_deep` except that it accepts `customizer` which is invoked to produce the cloned value.
719
720
**Parameters:**
721
- `value` (`Any`): Value to clone.
722
- `customizer` (`Callable`, optional): Function to customize cloning.
723
724
**Returns:**
725
- `Any`: Deep cloned value.
726
727
## Object Transformation
728
729
### transform
730
731
```python { .api }
732
def transform(obj: Any, iteratee: Callable = None, accumulator: Any = None) -> Any
733
```
734
735
An alternative to `reduce` for objects. This method transforms `obj` to a new accumulator object which is the result of running each of its own enumerable string keyed properties through `iteratee`.
736
737
**Parameters:**
738
- `obj` (`Any`): Object to iterate over.
739
- `iteratee` (`Callable`, optional): Function invoked per iteration.
740
- `accumulator` (`Any`, optional): Custom accumulator value.
741
742
**Returns:**
743
- `Any`: Accumulated value.
744
745
**Example:**
746
```python { .api }
747
from pydash import transform
748
749
transform([1, 2, 3, 4], lambda acc, val, key: acc.append(val * 2), [])
750
# [2, 4, 6, 8]
751
752
transform({'a': 1, 'b': 2, 'c': 1}, lambda acc, val, key: acc.setdefault(val, []).append(key), {})
753
# {1: ['a', 'c'], 2: ['b']}
754
```
755
756
### rename_keys
757
758
```python { .api }
759
def rename_keys(obj: Dict[Any, Any], key_map: Dict[Any, Any]) -> Dict[Any, Any]
760
```
761
762
Rename the keys of `obj` using the mapping in `key_map` and return new object.
763
764
**Parameters:**
765
- `obj` (`Dict[Any, Any]`): Object to rename.
766
- `key_map` (`Dict[Any, Any]`): Mapping from old keys to new keys.
767
768
**Returns:**
769
- `Dict[Any, Any]`: Object with renamed keys.
770
771
**Example:**
772
```python { .api }
773
from pydash import rename_keys
774
775
rename_keys({'a': 1, 'b': 2}, {'a': 'x', 'b': 'y'})
776
# {'x': 1, 'y': 2}
777
```
778
779
## Function Application
780
781
### apply
782
783
```python { .api }
784
def apply(obj: T, func: Callable[[T], T2]) -> T2
785
```
786
787
Apply function `func` to `obj`.
788
789
**Parameters:**
790
- `obj` (`T`): Object to apply function to.
791
- `func` (`Callable[[T], T2]`): Function to apply.
792
793
**Returns:**
794
- `T2`: Result of applying `func` to `obj`.
795
796
**Example:**
797
```python { .api }
798
from pydash import apply
799
800
apply([1, 2, 3], sum)
801
# 6
802
```
803
804
### apply_if
805
806
```python { .api }
807
def apply_if(obj: T, func: Callable[[T], T2], predicate: Callable[[T], bool]) -> Union[T, T2]
808
```
809
810
Apply function `func` to `obj` only if `predicate` returns `True` for `obj`.
811
812
**Parameters:**
813
- `obj` (`T`): Object to apply function to.
814
- `func` (`Callable[[T], T2]`): Function to apply.
815
- `predicate` (`Callable[[T], bool]`): Predicate to determine whether to apply function.
816
817
**Returns:**
818
- `Union[T, T2]`: Result of applying `func` to `obj` if predicate is truthy, else `obj`.
819
820
### apply_if_not_none
821
822
```python { .api }
823
def apply_if_not_none(obj: Optional[T], func: Callable[[T], T2]) -> Optional[T2]
824
```
825
826
Apply function `func` to `obj` only if `obj` is not `None`.
827
828
**Parameters:**
829
- `obj` (`Optional[T]`): Object to apply function to.
830
- `func` (`Callable[[T], T2]`): Function to apply.
831
832
**Returns:**
833
- `Optional[T2]`: Result of applying `func` to `obj` if `obj` is not `None`, else `None`.
834
835
### apply_catch
836
837
```python { .api }
838
def apply_catch(obj: Any, func: Callable, exceptions: Union[Exception, Tuple[Exception, ...]] = Exception, default: Any = None) -> Any
839
```
840
841
Apply function `func` to `obj` and catch any exceptions raised.
842
843
**Parameters:**
844
- `obj` (`Any`): Object to apply function to.
845
- `func` (`Callable`): Function to apply.
846
- `exceptions` (`Union[Exception, Tuple[Exception, ...]]`): Exception types to catch. Defaults to `Exception`.
847
- `default` (`Any`): Default value to return if exception is caught. Defaults to `None`.
848
849
**Returns:**
850
- `Any`: Result of applying `func` to `obj` or `default` if exception is caught.
851
852
### invoke
853
854
```python { .api }
855
def invoke(obj: Any, path: Union[str, List], *args: Any, **kwargs: Any) -> Any
856
```
857
858
Invokes the method at `path` of `object`.
859
860
**Parameters:**
861
- `obj` (`Any`): Object to query.
862
- `path` (`Union[str, List]`): Path of the method to invoke.
863
- `args` (`*Any`): Arguments to invoke the method with.
864
- `kwargs` (`**Any`): Keyword arguments to invoke the method with.
865
866
**Returns:**
867
- `Any`: Result of the invoked method.
868
869
**Example:**
870
```python { .api }
871
from pydash import invoke
872
873
obj = {'a': [{'b': {'c': [1, 2, 3, 4]}}]}
874
invoke(obj, 'a[0].b.c.pop', 1)
875
# 2
876
```
877
878
## Object Iteration
879
880
### for_in
881
882
```python { .api }
883
def for_in(obj: Any, iteratee: Callable = None) -> Any
884
```
885
886
Iterates over own and inherited enumerable properties of an object and invokes `iteratee` for each property.
887
888
**Parameters:**
889
- `obj` (`Any`): Object to iterate over.
890
- `iteratee` (`Callable`, optional): Function invoked per iteration.
891
892
**Returns:**
893
- `Any`: `obj`.
894
895
### for_in_right
896
897
```python { .api }
898
def for_in_right(obj: Any, iteratee: Callable = None) -> Any
899
```
900
901
Like `for_in` except that it iterates over properties in the opposite order.
902
903
**Parameters:**
904
- `obj` (`Any`): Object to iterate over.
905
- `iteratee` (`Callable`, optional): Function invoked per iteration.
906
907
**Returns:**
908
- `Any`: `obj`.
909
910
### callables
911
912
```python { .api }
913
def callables(obj: Any) -> List[Any]
914
```
915
916
Creates a list of function property names from own enumerable properties of `object`.
917
918
**Parameters:**
919
- `obj` (`Any`): Object to inspect.
920
921
**Returns:**
922
- `List[Any]`: List of function property names.
923
924
**Example:**
925
```python { .api }
926
from pydash import callables
927
928
class MyClass:
929
def method1(self): pass
930
def method2(self): pass
931
attr = 'value'
932
933
callables(MyClass())
934
# ['method1', 'method2']
935
```
936
937
## Type Conversion Functions
938
939
### to_boolean
940
941
```python { .api }
942
def to_boolean(obj: Any) -> bool
943
```
944
945
Converts `value` to a boolean.
946
947
**Parameters:**
948
- `obj` (`Any`): Value to convert.
949
950
**Returns:**
951
- `bool`: Converted boolean.
952
953
**Example:**
954
```python { .api }
955
from pydash import to_boolean
956
957
to_boolean(1)
958
# True
959
960
to_boolean(0)
961
# False
962
963
to_boolean('true')
964
# True
965
966
to_boolean('')
967
# False
968
```
969
970
### to_dict
971
972
```python { .api }
973
def to_dict(obj: Any) -> Dict[Any, Any]
974
```
975
976
Converts `value` to a dictionary.
977
978
**Parameters:**
979
- `obj` (`Any`): Value to convert.
980
981
**Returns:**
982
- `Dict[Any, Any]`: Converted dictionary.
983
984
**Example:**
985
```python { .api }
986
from pydash import to_dict
987
988
to_dict([1, 2, 3])
989
# {0: 1, 1: 2, 2: 3}
990
991
to_dict('abc')
992
# {0: 'a', 1: 'b', 2: 'c'}
993
```
994
995
### to_integer
996
997
```python { .api }
998
def to_integer(obj: Any) -> int
999
```
1000
1001
Converts `value` to an integer.
1002
1003
**Parameters:**
1004
- `obj` (`Any`): Value to convert.
1005
1006
**Returns:**
1007
- `int`: Converted integer.
1008
1009
**Example:**
1010
```python { .api }
1011
from pydash import to_integer
1012
1013
to_integer(3.2)
1014
# 3
1015
1016
to_integer('3')
1017
# 3
1018
1019
to_integer('abc')
1020
# 0
1021
```
1022
1023
### to_list
1024
1025
```python { .api }
1026
def to_list(obj: Any, split_strings: bool = True) -> List[Any]
1027
```
1028
1029
Converts `value` to a list.
1030
1031
**Parameters:**
1032
- `obj` (`Any`): Value to convert.
1033
- `split_strings` (`bool`): Whether to split strings into characters. Defaults to `True`.
1034
1035
**Returns:**
1036
- `List[Any]`: Converted list.
1037
1038
**Example:**
1039
```python { .api }
1040
from pydash import to_list
1041
1042
to_list({'a': 1, 'b': 2})
1043
# [1, 2]
1044
1045
to_list('abc')
1046
# ['a', 'b', 'c']
1047
1048
to_list('abc', split_strings=False)
1049
# ['abc']
1050
```
1051
1052
### to_number
1053
1054
```python { .api }
1055
def to_number(obj: Any, precision: int = 0) -> Union[float, None]
1056
```
1057
1058
Converts `value` to a number.
1059
1060
**Parameters:**
1061
- `obj` (`Any`): Value to convert.
1062
- `precision` (`int`): Precision to round to. Defaults to `0`.
1063
1064
**Returns:**
1065
- `Union[float, None]`: Converted number or `None`.
1066
1067
**Example:**
1068
```python { .api }
1069
from pydash import to_number
1070
1071
to_number('3.2')
1072
# 3.2
1073
1074
to_number('3.258', 2)
1075
# 3.26
1076
```
1077
1078
### to_string
1079
1080
```python { .api }
1081
def to_string(obj: Any) -> str
1082
```
1083
1084
Converts `value` to a string.
1085
1086
**Parameters:**
1087
- `obj` (`Any`): Value to convert.
1088
1089
**Returns:**
1090
- `str`: Converted string.
1091
1092
**Example:**
1093
```python { .api }
1094
from pydash import to_string
1095
1096
to_string([1, 2, 3])
1097
# '1,2,3'
1098
1099
to_string({'a': 1})
1100
# "{'a': 1}"
1101
```
1102
1103
### parse_int
1104
1105
```python { .api }
1106
def parse_int(value: Any, radix: Union[int, None] = None) -> Union[int, None]
1107
```
1108
1109
Converts `string` to an integer of the specified radix. If `radix` is falsy, a radix of `10` is used unless `value` is a hexadecimal, in which case a radix of `16` is used.
1110
1111
**Parameters:**
1112
- `value` (`Any`): String to convert.
1113
- `radix` (`Union[int, None]`): Radix to interpret `value` by.
1114
1115
**Returns:**
1116
- `Union[int, None]`: Converted integer.
1117
1118
**Example:**
1119
```python { .api }
1120
from pydash import parse_int
1121
1122
parse_int('08')
1123
# 8
1124
1125
parse_int('0x20')
1126
# 32
1127
1128
parse_int('08', 10)
1129
# 8
1130
1131
parse_int('1010', 2)
1132
# 10
1133
```
1134
1135
This Objects module provides comprehensive functionality for manipulating objects and dictionaries with 48 functions covering property access, transformation, cloning, merging, and type conversion operations.