duck-typing

Duck typing, must it be dynamic?

让人想犯罪 __ 提交于 2019-12-17 22:34:12
问题 Wikipedia used to say* about duck-typing: In computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. (* Ed. note: Since this question was posted, the Wikipedia article has been edited to remove the word "dynamic".) It says about structural typing: A structural

How to resolve the hardcoding when a newer version of the library i use uses a different function name? - python

核能气质少年 提交于 2019-12-13 20:12:26
问题 Is there a better way to resolve the problem of hardcoding compatiable code when a library i use uses a different name of the function? Also, I cannot change the library code. (because that function i'm using is EVERYWHERE in the old version of the code). The library is BeautifulSoup 3 and 4. see Method Name section in http://www.crummy.com/software/BeautifulSoup/bs4/doc/ Originally, i have bs4 code, but my users have bs3, so i have to put the following code everywhere: try: from bs4 import

Duck typing in Delphi 2007 (Continued)?

不问归期 提交于 2019-12-13 06:12:27
问题 This is a follow up to this post. I refined my requirement based on the accepted answer posted here. My *.dpr file: program DuckD11; {$APPTYPE CONSOLE} uses SysUtils, uDuckTyping in 'uDuckTyping.pas', uBirds in 'uBirds.pas'; procedure DoSomething(AObject: TObject); begin Duck(AObject).Quack; end; var Bird: TBird; Ganagana: TGanagana; Canard: TCanard; begin Writeln('Duck typing :'); Writeln; Bird := TBird.Create('Bird'); try DoSomething(Bird); finally Bird.Free; end; Ganagana := TGanagana

Comparable types with mypy

这一生的挚爱 提交于 2019-12-11 09:18:52
问题 I'm trying to create a generic class to express that a value has lower and upper bounds, and to enforce those bounds. from typing import Any, Optional, TypeVar T = TypeVar("T") class Bounded(object): def __init__(self, minValue: T, maxValue: T) -> None: assert minValue <= maxValue self.__minValue = minValue self.__maxValue = maxValue However, mypy complains that: error: Unsupported left operand type for <= ("T") Apparently typing module doesn't allow me to express this (although it looks like

Does this approach to Python duck-typing mixed with isinstance() make sense?

此生再无相见时 提交于 2019-12-11 07:17:58
问题 Let's say we have the following classes: class Duck(object): pass class OldFashionedDuck(Organism, Duck): def look(self): self.display_biological_appearance() def walk(self): self.keep_balance_on_two_feet() def quack(self): self.make_noise_with_lungs("Quack!") class ArtificialDuck(Robot, Duck): def look(self): self.display_imitation_biological_appearance() def walk(self): self.engage_leg_clockwork() def quack(self): self.play_sound("quack.au") In this example, OldFashionedDuck and

“Iterating” over an async method

谁都会走 提交于 2019-12-11 02:49:33
问题 A few related questions about the async CTP: I can iterate over an Iterator Block (an IEnumerable<T> yield-returning T ) using GetEnumerator() and then enumerator methods MoveNext() , and Current() . What is the analog for async methods? How can a non- async calling method to receive and process any await ed items and then ContinueWith() ? Can you provide a short example? I'm just not seeing it. Also, in this following example async method, MyAwaitable has a GetAwaiter() method. If GetAwaiter

Simulating aspects of static-typing in a duck-typed language

丶灬走出姿态 提交于 2019-12-10 12:49:41
问题 In my current job I'm building a suite of Perl scripts that depend heavily on objects. (using Perl's bless() on a Hash to get as close to OO as possible) Now, for lack of a better way of putting this, most programmers at my company aren't very smart. Worse, they don't like reading documentation and seem to have a problem understanding other people's code. Cowboy coding is the game here. Whenever they encounter a problem and try to fix it, they come up with a horrendous solution that actually

What does duckmap really do?

谁说我不能喝 提交于 2019-12-10 02:18:57
问题 From the documentation duckmap will apply &block on each element and return a new list with defined return values of the block. For undefined return values, duckmap will try to descend into the element if that element implements Iterable . But then: my $list = [[1,2,3],[[4,5],6,7]]; say $list.deepmap( *² ); # [[1 4 9] [[16 25] 36 49]] say $list.duckmap( *² ); # [9 9] deepmap behaves pretty much as expected, but I can't really make any sense of what duckmap is doing. This question is related

What is a Refused Bequest?

孤街浪徒 提交于 2019-12-10 01:36:41
问题 Could someone please explain what does Refused Bequest means? I tried reading some articles and says its a kind of code smell or in wiki it tells that it is a class that overrides a method of a base class in such a way that the contract of the base class is not honored by the derived class. But in a nutshell or in a more simple terms, what is it actually? 回答1: I think you get it. Refused Bequest is a code smell. But, what type of code smell? Quoting Martin Fowler's book Refactoring: improving

Pythonic way to verify parameter is a sequence but not string

别等时光非礼了梦想. 提交于 2019-12-09 17:59:08
问题 I have a function that gets a list of DB tables as parameter, and returns a command string to be executed on these tables, e.g.: pg_dump( file='/tmp/dump.sql', tables=('stack', 'overflow'), port=5434 name=europe) Should return something like: pg_dump -t stack -t overflow -f /tmp/dump.sql -p 5434 europe This is done using tables_string='-t '+' -t '.join(tables) . The fun begins when the function is called with: tables=('stackoverflow') (a string) instead of tables=('stackoverflow',) (a tuple),