Tag Info

New answers tagged

9

A test like hassattr(tweet, 'setdefault') to make sure tweet is a dictionary is not a good one, since it obviously does not assure tweet provides all methods/properties of a dictionary. So as long tweet.setdefault is not the only method called by find_closest_state (which I think is unlikely), this test is not strict enough. On the other hand, a test like ...


4

First, I want to say that by far not everybody agrees that duck typing is a good thing at all, let alone that it is some sort of holy principle that should be followed. Often duck typing leads to error prone, hard to debug code, although it can be very flexible in how you can use it. Duck typing just takes an object and does with the object what it wants to ...


0

Here is one way to do it, which increments a collections.Counter every time a callable attribute is accessed: class LoggingMixIn: def __init__(self, *args, **kwargs): self._log = Counter() super().__init__(*args, **kwargs) def __getattribute__(self, name): attr = super().__getattribute__(name) if callable(attr): ...


1

As you can see in the console, you're looking at bar, a function with the body shown. Within that are a number of properties available on function objects. If you care about how foo is stored, check out this article on V8's isomorphism and structure optimization. Looking at the MDN docs for function, you can see the same list of properties. arguments ...



Top 50 recent answers are included