__init__
defines the initialization behaviour of an object, but it is not the first thing called (that's__new__
)__new__
is the class constructor, passing the new class to__init__
for initialization; mostly useful if subclassing immutable type__del__
is the class destructor, when the object is garbage collected (for objects that require extra cleanup after deletion); not reliable if the interpreter exits so good cleanup practices are still required__cmp__
is the basic c comparison (negative result means it's less, etc; only in Python2)__eq__
,__ne__
,__lt__
,__gt__
,__le__
,__ge__
define behaviour for their operatorsfunctool
's@total_ordering
automates defining comparison methods- Unary operators:
__pos__
,__neg__
,__abs__
,__invert__
,__round__
,__floor__
,__ceil__
,__trunc__
- Arithmetic operators:
__add__
,__sub__
,__mul__
,__floordiv__
(//
),__div__
(/
; only in Python2),__truediv__
,__mod__
,__divmod__
,__pow__
(**
),__lshift__
,__rshift__
,__and__
,__or__
,__xor__
- Reflective arithmetic operators (object as second argument to operator): just append
r
to the arithmetic operators; usually can just call their normal operator - Augmented assignment (operator + assignment operation): just append
i
tot he arithmetic operators - Type conversion:
__int__
,__long__
,__float__
,__complex__
,__oct__
,__hex__
,__index__
(when object is used in a slice operation),__trunc__
,__coerce__
(only in Python2) - Class representations:
__str__
(for humans, defaults to__repr__
)__repr__
(for use in code and interpreters__unicode__
(doesn't work whenstr()
is used on class; only in Python2)__format__
(for use instr.format()
)__hash__
(for use withhash()
; usually for dicts along with__eq__
)__nonzero__
(forbool()
; renamed to__bool__
in Python3)__dir__
(should return list of attributes for the user; usually unnecessary)__sizeof__
- Controlling attribute access
- Encapsulation in Python comes through "magic", rather than explicit modifiers
__getattr__
is called when a nonexistsent attribute is accessed; can be used for misspellings, deprecated attributes, or raisingAttributeError
s__setattr__
allows you to define custom rules for any changes in the values of attributes__delattr__
is similar to__setattr__
but for deleting attributes__getattribute__
is similar to__setattr__
but for getting attributes. Only works with new style classes, but can be tricky to implement- Implementing these can be tricky and can easily lead to infinite recursion
- Custom sequences
- Immutable containers only need
__len__
and__getitem__
defined - Mutable containers require
__setitem__
and__delitem__
as well - Iterable containers require
__iter__
- Others:
__reversed__
,__contains__
,__missing__
(only for subclasses of dict)
- Immutable containers only need
- Reflection:
__instancecheck__
,__subclasscheck__
- Callable objects:
__call__
allows classes to be callable (as if they were functions!) - Context managers:
__enter__
,__exit__
- Descriptors:
__get__
,__set__
,__delete__
- Copying:
__copy__
,__deepcopy__
- Pickling:
__getinitargs__
(only for old style classes in Python2),__getnewargs__
,__setstate__
,__reduce__
- Nesting anchors can require
<object>
tags around the nested anchor (browser short circuits on nested anchors) - Can use
<object type="image/svg+xml" data="image.svg">
for SVGs :before
and:after
works (sometimes) with<img>
elements- CSS
calc()
is amazing- Calculate font-sizes with vw and vh to create fluid font-sizes based on viewport size
- Also for line heights
- Calculate column widths and then apply
min-width
ormax-width
- Calculate the necessary overflow padding from a container (e.g. if you want a child element to be full width when nested in a container with margin)
- Calculate font-sizes with vw and vh to create fluid font-sizes based on viewport size
- Make sure your emails are bulletproof (test with images turned off!)
- Use progressive scans of images
- Use
table-layout:fixed
to fix the layout of the table based on the first row - Transitions don't work on
min-height
- Can override platform-specific emoji designs by using your own fonts that override the Unicode code points
- With HTTP2, multi-stage CSS loading removes the need for critical CSS
<img>
tags can use aobject-fit
CSS property to be similar tobackground-size: contain
and etc- CSS
currentColor
represents the calculated value of the element'scolor
property - CSS
contain
allows you to define priorities for loading and painting CSS
- When checking
if
on a variable multiple times, use an iterable:if x in (1, 2, 3):
- Dict comprehensions also exist
- So do set comprehensions
- Sets have overloaded operators:
A | B
== union;A & B
== intersect;A - B
== difference;A ^ B
: symmetric difference - For longer lists, use a generator expression instead of a list comprehension to save memory
- Use
sys.exit()
in CLI scripts to return error codes; 0 means everything went well- The code after your
if __name__ == '__main__':
should only ever besys.exit(main())
- The code after your
- Don't use relative imports
- Use the
os.path
module for file system paths
- Use decorators
- Use context managers
- Most basic types (and tuples) are immutable; this includes strings
- To concat a number of strings, list comprehension into array and then
"".join(list)
- Pythonic style:
- Use keyword arguments generously to aid meaning
- Don't change how core Python works unless you absolutely need to (e.g. object creation, imports, etc)
- Put error checking returns at the top
- Preferable to keep a single exit point
- Use variable unpacking
- Use
__
as an ignored variable when unpacking (to avoid clashes with_
) - Length-N list of the same thing:
val = [None] * 5
- Length-N list of lists:
val = [[] for __ in range(4)]
(xrange was renamed to
range` in Python 3) - Python's
set
s (anddict
s) are hashtables; lookup usingin
is much more efficient than in alist
- Use
in
or a default argument todict.get()
instead ofdict.has_key()
- Use
enumerate
to get the list index while usingfor in
- Use parenthesed around multi-line statements to let the interpreter join the lines until the parentheses are closed
- Numeric zeroes, empty sequences, empty dicts, and objects whose defined
__bool__()
or__len__()
methods return falsey values - Use
not
instead of!
to revert logic - Documentation
- Use docstrings
- Do not use trip-quote strings for block-level comments
- Logging
- The user should dictate what happens when a logging event occurs, not the library
- Instatiate loggers in a library by only using the
__name__
variable; this ensures no name collisions
- Gotchas
- Default arguments are created once during function definition, AND CAN BE MUTATED
- Use a default argument to signal that no argument was provided to create a new object instead
- Can be useful as a way to define static variables (i.e. to cache state between calls)
- Closures are late binding; they only look up enclosed variables when the closure is actually invoked
- Mostly felt when looping to create unique functions
- Instead, bind a local variable or default parameter to the indended enclosed variable
.pyc
files are automatically generated and should be ignored from version control- Use
PYTHONDONTWRITEBYTECODE=1
to disable this behaviour
- Use
- Not supplying a license: "In the US, if no license is specified, users have no legal right to download, modify, or distribute."
- Default arguments are created once during function definition, AND CAN BE MUTATED
- Web scraping
- Use
lxml
to parse XML and HTML
- Use
- Performance
- CPython is slow
- The GIL (Global Interpreter Lock) prevents multi-threaded Python; necessary as CPython's memory management is not thread-safe
- Maximizing performance requires a strong understanding of the GIL
- Running another Python instance in another process does not run into this limitation (see
ThreadPoolExecutor
andProcessPoolExecutor
- Format:
min hr dom month dow cmd
crontab -e
loads your crontab file in your$EDITOR
pip freeze > requirements.txt
is pretty rad
- Never
except Exception
and pass, as it hides all errors - At the least, log the exception and its stack trace
- Only silently pass specific exception classes that you know about in advance
- Make sure your code isn't beautiful but bad!
- Don't let PEP8 / etc cloud your vision (look for Pythonic vs Non-Pythonic instead); pay attention to things that really matter!
- Use adapter patterns to wrap bad packages with good interfaces!
- Prefer Python's context managers for setup / cleanup code (e.g.
__enter__
,__exit__
) - Use Python's property descriptors rather than getters / setters
- Use iterables when you have iterables
- Avoid unnecessary packaging
- Use magic methods (e.g.
__len__
,__getitem__
) - Use
__repr__
- Wrap bad exceptions with good ones
- Stop thinking in indicies
- Use
enumerate
to get foreach indicies - Use
izip
to loop over multiple collections (iterator'dzip
) and dict creation out of two lists - Better performance: make sure your code is running in L1 cache!
- Use
key
functions to sort, instead of comparision functions iter
's second parameter is a sentinel valuefor-else
exists- Use
dict.keys()
to iterate over a dict if you'll be mutating it - Use
dict.iterItems()
for an iterable over the dict - Use
defaultdict
for dictionaries with defaults (needs to be converted back to a dict sometimes) - Use
namedtuple
s! - "Don't break atoms of thought into subatomic particles"
- Prefer easy things over complicated things
- "I hate code and I want as little of it as possible in our product"
- Ship features not code
- If you think you need it later, do it later
- Namespaces are not for creating taxonomies; use them to prevent name collisions
- Interesting retrospective on current and past identity schemes
- Sets out a number of criteria to judge strength identity schemes
- Modern
setImmediate()
s have a 4ms timer; using 0, 1, 2, 3, or 4 mean the same thing requestAnimationFrame()
is more useful for animations; it requests a new UI update after the current onesetImmediate()
inserts the task after the last paint event in the current event loop- Node has
process.nextTick()
as a similar API - Polyfills needed for use
- Secure (full end-to-end encryption), mesh networking protocol
- Mesh: encrypted sessions (links) between any two endpoints
- UI as a pure function of application state
- "When the magnitude of the undertaking is not clear for the developer or designer, let alone for the people removed from the creation process, it’s simply impossible to make any accurate predictions."
- Break designs into their smallest possible variations and implement against those
- "A distinction of two very distinct (yet frequently interleaved) phases of the creation process: design and discovery"
- Design, discover all possible variations, design, rediscover, etc.
- "Applications must handle different states that are difficult to replicate under normal testing conditions."
- Keeping random GUI windows afloat (i.e. always on top) on OSX is possible
- Don't add boolean parameters
- Prefer obvious, keyword, or human-readable arguments instead
- Chaos testing
- Most common for file formats, network protocols
- Most useful when testing input that crosses a trust protocol (i.e. user-generated content)
- Long list of wrong myths about people and their names
- Write down your top 25 career goals
- Circle the top 5 goals
- Eliminate the rest. "Everything you didn’t circle just became your Avoid-At-All-Cost list. No matter what, these things get no attention from you until you’ve succeeded with your top 5.”
- Immediately do urgent and important tasks
- Schedule for later important but not urgent tasks
- Delegate urgent but not important tasks
- Eliminate neither urgent nor important tasks
- Do anything that only takes two minutes immediately
- Any new habit should be started within two minutes, let momentum carry you to do it further
- Look at a problem and think about the exactly opposite of what you want to achieve
- E.g. Instead of how to be a better manager, ask what would a terrible manager do?
- "It is far easier to avoid stupidity than it is to create genius."
- "Point of View is worth 80 IQ points"
- Develop multiple perspectives to look at a problem, and select the best for the problem at hand
- Developing perspectives:
- Read and do things outside of the norm
- Link separate, seemingly disparate ideas with others
- Think from first principles, not analogies
- Build up from fundamental, hard facts to solve problems
- “If you don’t fall, you’re not trying hard enough.”
- Amygdala rewiring === rewiring fight, flight, or freeze instinct
- Goal is to remove differences as much as possible to make the listener feel comfortable to be engaged
- Goal of management is a high performing team, focused on outputs, which can only be coordinated by communication
- Meta models for effective communication
- Toward vs. Away: Optimist vs pessimest
- Toward: motivated to attain goals, use words like "gain", "obtain", "achieve", ...
- Away: motivated to solve problems, use words like "avoid", "exclude", ...
- Frame actions as either moving away from something or towards another
- Internal-referencing vs External-referencing: Internal vs external motivators
- Internal: decisions are based on their own self, use language like "you decide", "what do you think"
- External: seek external information and feedback from others, use language like "feedback", "approval", "others..."
- How do you know you've done a good job at x
- Specific vs General: Details vs overview
- Specific: operate with details, use words like "exactly", "precisely"
- General: focus on overview, use language like "essentially", "generally"
- See, Heard, Read, Do: Vector of convincing and persuasion
- See: influenced by what they perceive (55%)
- Hear: influenced by what they hear (30%)
- Do: influenced by past and current action (12%)
- Read: influenced by what they read (3%)
- How do you know that someone else is good at their job?
- Toward vs. Away: Optimist vs pessimest
- "You want people who choose to follow because they genuinely believe in ideas, not because they’re afraid to be punished if they don’t."
- Seeds a resilient culture
- Hire as many originals as possible
- Look for:
- Unsung heroes
- Insubordinates
- Inward-facing innovators
- Interviewing:
- Do they default to challenging? Can they challenge the quo?
- "Why shouldn't I hire you?"
- “Originals are constructive contrarians. They're not just pointing out that the emperor has no clothes; they're also tailors.”
- Look for diversity, curiosity
- Find out what they've tried but given up; "their roads not taken"
- Force references to be candid: make them choose between two seemingly undesirable choices
- Constant learners can adjust to any new environment or role quickly
- "When you boil it down, learning is about changing your mind"
- Gamer: Take risks, be optimistic, keep trying
- Clarity of objectives empowers individuals to be resourceful
- "How would we approach this if it were a game?"
- "The gameful leader expects adversity, but remains excited about solving the puzzle."
- Beginner: Be childlike and curious
- Ask "why not" and "what if"
- Reason from first principles
- Take "yourself out of autopilot to shift into awe, curiosity, and wonder"
- "What would I think about this if I had no biases, or if I had never seen a situation like this before?"
- “What do I believe that the rest of the world doesn’t?”
- "Create an inner alarm to sound the moment you have the thought: ‘Of course, this the way to do it!’ Silence it by stepping back and reassessing the situation."
- "The only way to avoid bias and to stay open to the possibilities of learning, is vigilance."
- "Value asking questions and people who ask questions"
- "What have you learned in this conversation?"
- Growth: Your performance is always a reflection of your current self, which will grow in the future
- Relish in taking on challenges
- Possess a self-motivated passion for learning; always be in self-improvement and self-actualization mode
- "Doing the right thing is more important than doing things right."
- Premature perfectionism is the root of all evil. Get the feedback you need immediately.
- Be around constant learners, always look to hire constant learners
- References: ‘Please call me back if this is an outstanding candidate for a job that requires learning.’
- "Hire people for the way they approach problems."
- Make it clear to new team members you believe they'll make a difference and will get to grow a lot
- Help new team members set goals so they can experience wins early on and constantly
- "We’re not knowledge workers. We’re learning workers."
- Put away the pencil to write code
- 0-30: Educate
- 31-60: Find rhythm and style
- 61-90: Assess this is a good fit for you
- Management is in all three directions: above, beside, and below
- "You no longer ride on the rollercoaster. But you operate it."
- Manage only if that's what you want, if you can channel empathy, and if you can give trust
- Figure out which are the important meetings, and cancel others
- Build an event loop / pipeline for the current day, week, month, quarter
- If this isn't for you, it's OK to step down after trying it!
- "Engineers are hired to create business value, not to program things"
- "Nobody ever outsources Profit Centers"
- Describe your accomplishments by increasing revenues or decreasing costs
- Strive to help people
- Most important professional skill is communication
- "Modesty is not a career-enhancing character trait"
- Salary negotiations are important
- "Rich, successful people negotiate
- Start salary negotiations after all interviews and a fit looks possible
- Never give up your number first; use any number of excuses
- Repeat other people's words back to them for convincing; take notes!
- Stress "we" or "you" benefits
- Use things other than the salary to negotiate
- Use some new information to convince others you're valuable
- Interviewing is a terrible medium for hiring
- Interviews are hostile environments for candidates
- Interviews are biased twoards candidates who are good at interviews
- Ideas:
- Warm up candidates. Welcome them with an initial call. Send them material to review.
- Build a standardized interviewing format for every interviewer to follow. Interviewers should hate it! Use a script.
- Make sure the standardized format generates data, and is testable.
- Strip existing systems and let the candidate build part of it back up
- Collect objective facts for causation metrics (when looking back)
- Score candiates using the same rubric
- Remove tests that don't predict well
- Avoid pressured situations
- Checklist:
- Is it consistent? Does every candidate get the same interview?
- Does it correct for hostility and pressure? Does it factor in “confidence” and “passion”? If so, are you sure you want to do that?
- Does it generate data beyond hire/no-hire? How are you collecting and comparing it? Are you learning both from your successes and failures?
- Does it make candidates demonstrate the work you do? Imagine your candidate is a “natural” who has never done the work, but has a preternatural aptitude for it. Could you hire that person, or would you miss them because they have the wrong kind of Github profile?
- Are candidates prepped fully? Does the onus for showing the candidate in their best light fall on you, or the candidate?
- RP3 has wifi!!!
- Cool available starter packs
- Design is becoming homogenized across the world, digital globalization (of taste) is somewhat of an apt description (or at least, a cause)
- Tasteful, but generic and empty; made to make changing places as frictionless as possible
- An anchoring to a recognizable home in an unfamiliar environment
- Placelessness and digital nomads are starting to become themes
- The gentrification of taste; depersonalization and the loss of identity
- "AESTHETIC HOMOGENEITY IS A PRODUCT THAT USERS ARE COMING TO DEMAND"
- URBAN AREAS AROUND THE WORLD MIGHT INCREASINGLY RESEMBLE EACH OTHER AND BECOME INTERCHANGEABLE