What is Interface Duck Typing?

C# has a nominal type system, so the compatibility of types is done based on their names. In your example you have two classes with a Quack method, however there is no way to write a method which can take instances of these two classes and invoke their Quack method. In C# 2, the solution … Read more

What is the difference between copying and cloning?

There’s no formal definition of these concepts, atleast not one that spans all languages. What’s usually common though: clone – create something new based on something that exists. copying – copy from something that exists to something else (that also already exists).

Is Clojure object-oriented at its heart? (Polymorphism in seqs)

Idiomatic Clojure favors defining independent functions that operate on a very small set of core data structures; this unbundling of methods and data is a strong statement against object orientation and in favour of a functional style. Rich Hickey (creator of Clojure) has repeatedly stated the importance of this; for example here: “Clojure eschews the … Read more

Performance of using static methods vs instantiating the class containing the methods

From here, a static call is 4 to 5 times faster than constructing an instance every time you call an instance method. However, we’re still only talking about tens of nanoseconds per call, so you’re unlikely to notice any benefit unless you have really tight loops calling a method millions of times, and you could … Read more

Recursively access dict via attributes as well as index access?

Here’s one way to create that kind of experience: class DotDictify(dict): MARKER = object() def __init__(self, value=None): if value is None: pass elif isinstance(value, dict): for key in value: self.__setitem__(key, value[key]) else: raise TypeError(‘expected dict’) def __setitem__(self, key, value): if isinstance(value, dict) and not isinstance(value, DotDictify): value = DotDictify(value) super(DotDictify, self).__setitem__(key, value) def __getitem__(self, key): … Read more

Accessing class member variables inside an event handler in Javascript

Since this changes in an event context (points to global usually), you need to store a reference to yourself outside of the event: function Map() { this.x = 0; this.y = 0; var _self = this; $(“body”).mousemove( function(event) { _self.x = event.pageX; // Is now able to access Map’s member variable “x” _self.y = event.pageY; … Read more