background preloader

A Guide to Python's Magic Methods « rafekettler.com

A Guide to Python's Magic Methods « rafekettler.com
Rafe Kettler Copyright © 2012 Rafe Kettler Version 1.17 A PDF version of this guide can be obtained from my site or Github. Table of Contents Introduction This guide is the culmination of a few months' worth of blog posts. What are magic methods? So, to fix what I perceived as a flaw in Python's documentation, I set out to provide some more plain-English, example-driven documentation for Python's magic methods. I hope you enjoy it. Construction and Initialization Everyone knows the most basic magic method, __init__. __new__(cls, [...) __new__ is the first method to get called in an object's instantiation. __init__(self, [...) The initializer for the class. __del__(self) If __new__ and __init__ formed the constructor of the object, __del__ is the destructor. Putting it all together, here's an example of __init__ and __del__ in action: Making Operators Work on Custom Classes if instance.equals(other_instance): # do something if instance == other_instance: #do something Comparison magic methods __pow__

ATM :: CheckIO Sofia found Stephen working in the garden. "Stephen!" she shouted. "No." he replied gloomily. "What was that?" "I said NO! "But I haven’t even said anything yet…" "And I don’t want to hear anything! "Come on, it was fun!" "It was fun right up until you caused a quantum fluctuation that sucked my Dads truck into a BLACK HOLE!" "You’re the one who let me drive it!" "Oh, so it’s my fault?" "Well, I’ve only driven ships..." Stephen stopped weeding the garden and looked up at Sofia. "What kind of ship?" " A spaceship. "I see..." Sofia let Stephen continue rambling and grumbling because his gripes were extremely entertaining to her. "There's an old merchant-bot on the island who is selling his old ship. Stephen didn’t respond. "There are five massive islands surrounding us. "Where would you get the money to buy the old ship?" "I can use my new credit card." "But you don’t even know how to use an ATM." "Oh, I'm sure it's SUPER complicated…" Teach Sofia how to use an ATM. Example:

Teach Yourself C++ in 21 Days Day 16 Streams Until now, you've been using cout to write to the screen and cin to read from the keyboard, without a full understanding of how they work. Today, you will learn What streams are and how they are used. Overview of Streams C++ does not, as part of the language, define how data is written to the screen or to a file, nor how data is read into a program. The advantage of having the input and output kept apart from the language and handled in libraries is that it is easier to make the language "platform-independent." NOTE: A library is a collection of OBJ files that can be linked to your program to provide additional functionality. Encapsulation The iostream classes view the flow of data from your program to the screen as being a stream of data, one byte following another. One principal goal of streams is to encapsulate the problems of getting the data to and from the disk or the screen. Figure 16.1. Buffering Figure 16.2. Figure 16.3. Figure 16.4. Figure 16.5. Streams and Buffers

Programming miscellany ↓ Skip to Main Content Programming miscellany Home › Software development › Programming miscellany Programming miscellany Posted on by John Here are a few of my favorite programming-related links that I’ve run across lately. ‹ Product of normal PDFs Euler characteristic with dice › Tagged with: Programming Posted in Software development One comment on “Programming miscellany” Stefan van der Walt says: 31 October 2012 at 01:23 Thanks for the link to the functional programming talk. John D. Subscribe RSS Subscribe by Email Recent Posts Categories Tags AppleBayesianBiostatisticsBooksBusinessC++CancerCreativityCSharpDifferential equationsEconomicsEducationEmacsGeodesyGraphicsHistoryHTMLIntegrationInterviewLaTeXMathMS OfficeMusicNumber theoryOptimizationPerlPowerShellProbability and StatisticsProductivityProgrammingPythonQualityQuotesRegular expressionsReproducibilityRstatsScienceSciPySimplicitySpecial functionsSymPyTwitterTypographyUnicodeWindows Archives © 2014 The Endeavour

ABC: Always Be Coding — Tech Talk Be honest. Are you a good engineering candidate? How are you measuring yourself? How many companies have you interviewed at? What is your onsite-interview to offer ratio? Try the following formula (that I’ve totally made up in a vacuum and ultimately means nothing): # x = number of companies interviewed with onsite# y = number of offers receivedvalue = 100 * log(x) * y / x If your value is < 90, you should read this. Who am I? I don’t have a college degree. Since then I’ve worked at Double Helix, Namco Bandai, Google, Obvious and Square.* I’ve also received offers from companies such as Naughty Dog, Activision, Riot Games, Blizzard, Pinterest, Goldman Sachs, and more. I’ve interviewed at least 500 engineering candidates. I will tell you that there is absolutely no sure-fire way to getting hired. The best I can do is tell you how you can be adequately prepared. Technical Tips ABC (Always Be Coding). General Tips I can’t claim to be an expert here. Know why you’re there.

Flex SDK / Wiki / Coding Conventions Revolutions Friday Q&A 2012-08-24: Things You Never Wanted To Know About C Friday Q&A 2012-08-24: Things You Never Wanted To Know About C It's been a bit since I did an article, but I'm back again, with a somewhat off-the-cuff treatment of a very twisted set of code I use to pretend that C has exceptions. I delve into little-known extensions of C, Linux compatibility, and worst of all, goto, so be warned! Error handling in CC, being something of a bare-metal language by today's standards, lacks any advanced facilities for managing control flow when errors happen. ExceptionsThe best known, and also perhaps most debated, means of handling control flow for error conditions is exceptions. Fortunately, Apple realized how ridiculous this tends to look in Cocoa and deprecated exceptions for error handling, instead opting for the NSError model. C does have a primitive sort of exception handling in the setjmp/longjmp functions. Grafting exceptions onto CThe first question asked is why anyone would want to put exceptions into pure C. That last is a doozy. Yuck, huh?

Syntax Highlighting on Blogger - Gist Here is another way to paste code into blogger with nice syntax highlighting.Go to the code you want to postCopy the embed tagGo back to blogger, and select "Edit Html"Paste the embed tag into your postBelow is the example source code using this tag: A potential drawback of this approach might be the stability of github as a company. In a downturn, it may shut down with very short notice. Compare to using SyntaxHighlighter, personally, I think this approach is way better: No setup required (in a previous post I documented how to setup syntaxhighlighter for blogger, it took me half an hour to setup everything correctly. To paste script like below used to require replacing '<' with &lt; and '>' with &gt; manually.

The birth of LISP – a summary of John McCarthy’s original paper A programming system called LISP (for LISt Processor) has been developed for the IBM 704 computer by the Artificial Intelligence group at M.I.T. The system was designed to facilitate experiments with a proposed system called the Advice Taker, whereby a machine /../ could exhibit “common sense” in carrying out its instructions./../LISP eventually came to be based on a scheme for representing the partial recursive functions of a certain class of symbolic expressions. It now seems expedient to expound the system by starting with the class of expressions called S-expressions and the functions called S-functions. With that, John McCarthy starts off his Recursive Functions of Symbolic Expressions and Their Computation by Machine paper. Two years after LISP was originally developed, long before many of the concepts presented in the paper become the norm. Garbage collection, for instance, was first proposed/described in this paper (invented in 1959, paper published in 1960). Soup. List structure

Related: