Skip to main content

Home/ Mac Attack/ Group items tagged reference

Rss Feed Group items tagged

Benjamin Bandt-Horn

Python: How do I pass a variable by reference? - Stack Overflow - 0 views

  • Parameters are passed by value
  • some data types are mutable, but others aren't
  • If you pass a mutable object into a method, the method gets a reference to that same object and you can mutate it to your heart's delight, but if you rebind the reference in the method, the outer scope will know nothing about it, and after you're done, the outer reference will still point at the original object.
  • ...5 more annotations...
  • List - a mutable type
  • String - an immutable type
  • you could return the new value. This doesn't change the way things are passed in, but does let you get the information you want back out:
  • use_a_wrapper_to_simulate_pass_by_reference
  • But sometimes the thing was a pointer
  •  
    Parameters are passed by value
Benjamin Bandt-Horn

3. Data model - Python v2.6.6 documentation - 0 views

  • it is advised to somehow mix together (e.g. using exclusive or) the hash values for the components of the object that also play a part in comparison of objects.
  • If a class does not define a __cmp__() or __eq__() method it should not define a __hash__() operation either
  • Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute nor is it found in the class tree for self). name is the attribute name. This method should return the (computed) attribute value or raise an AttributeError exception.
  • ...2 more annotations...
  • new-style classes
  • By default, instances of both old and new-style classes have a dictionary for attribute storage
  •  
    special method names
Benjamin Bandt-Horn

operators - What is the name of ** in python? - Programmers Stack Exchange - 0 views

  • It's not an operator as such, so it doesn't really have a name, but it is defined as a "syntactic rule". So it should be called: "the keyword argument unpacking syntax"
  • # usually a tuple, always an iterable*
  • # usually a dict, always a mapping*
  • ...4 more annotations...
  • kwargs
  • args =
  • *: Iterables are objects that implement the __iter__() method and mappings are objects that implement __iter__() and __getitem__()
  • If you are unsure what to call a particular operator or if it is unnamed, you can always resort to Waka Waka Bang Splat as a reference to help you figure out what to call it. In this case for ** I would call it double-splat, though there are some alternate names for symbols.
  •  
    It's not an operator as such, so it doesn't really have a name, but it is defined as a "syntactic rule". So it should be called: "the keyword argument unpacking syntax"
Benjamin Bandt-Horn

Mac OSX : How to burn an ISO image to a USB key - The Endless Geek - 0 views

  •  
    If you have tried to create a bootable USB key from an iso image in Mac OSX using Disk Utility then you have probably encountered the frustrating and almost Windows-like cryptic error message "Could not validate source - Invalid argument" error. Looking in the system log via the Console app I suspect this is because internally DiskUtil tries to run /usr/sbin/asr to verify the image, which fails.   1 2 3 $ asr imagescan --source ubuntu-rescue-remix-12-04.iso only UDIF and NDIF images can be scanned. asr: image scan failed - Invalid argument. UDIF and NDIF are image formats used by Apple, and Disk Utility is pretty hopeless with anything that falls outside of these standards. The iso standard is short for ISO9660 and is a standard that defines the format of an image intended for burning to CD. Even after using hdutil to convert the image to UDRO (a UDIF Read-Only image) Disk Utility will still stubbornly refuse to help. Disk Destroyer Duplicator to the rescue Being Unix based, OSX has the command line dd utility available. Short for Disk Duplicator, dd is a block level reader/writer that makes raw copies from one file to another. But you want to copy the image to a device, right? That's fine, because everything in the world of Unix/Linux is a file - even devices. Informally referred to as Disk Destroyer, should you tell dd to output to the wrong device then your day is definitely going to be spoiled, so to avoid any mishaps we will make sure we know which devices on your system is your USB stick. You can determine this from the command line:
Benjamin Bandt-Horn

4. More Control Flow Tools - Python v2.7.6 documentation - 0 views

  • *name must occur before **name.
  • keys = sorted(keywords.keys()) for kw in keys: print kw, ":", keywords[kw]
  • Note that the list of keyword argument names is created by sorting the result of the keywords dictionary’s keys() method before printing its contents; if this is not done, the order in which the arguments are printed is undefined
  • ...11 more annotations...
  • They are syntactically restricted to a single expression
  • Like nested function definitions, lambda functions can reference variables from the containing scope
  • Coding Style
  • CamelCase for classes
  • lower_case_with_underscores for functions and methods
  • Always use self as the name for the first method argument
  • comments
  • docstrings
  • separate
  • 79 characters
  • 4-space indentation, and no tabs
  •  
    keys = sorted(keywords.keys()) for kw in keys: print kw, ":", keywords[kw]
Benjamin Bandt-Horn

The Architecture of Open Source Applications: Python Packaging - 0 views

  • There are two schools of thought when it comes to installing applications
  • each application is its own standalone "appliance", and installing and removing them should not disturb the rest of the OS. If the application needs an uncommon library, that library is included in the application's distribution
  • a collection of small self-contained units called packages
  • ...8 more annotations...
  • Libraries are bundled into packages
  • Self-contained applications also make the developer's life easier when she needs to support several operating systems. Some projects go so far as to release portable applications that remove any interaction with the hosting system by working in a self-contained directory, even for log files.
  • Fellowship of the Packaging
  • In Python a package is a directory containing Python files. Python files are called modules. That definition makes the usage of the word "package" a bit vague since it is also used by many systems to refer to a release of a project.
  • use the term "Python packages" when we talk about a directory containing Python modules
  • The term "release" is used to define one version of a project
  • "distribution" defines a source or a binary distribution of a release as something like a tarball or zip file
  • advanced tools like Setuptools, which add features on the top of it, or Distribute, a fork of Setuptools. There's also Pip, a more advanced installer, that relies on Setuptools
  •  
    There are two schools of thought when it comes to installing applications
Benjamin Bandt-Horn

Elegant ways to support equivalence ("equality") in Python classes - Stack Overflow - 0 views

  • mixin class
  • Another issue with the __dict__ comparison is what if you have an attribute that you don't want to consider in your definition of equality (say for example a unique object id, or metadata like a time created stamp).
  • isinstance sucks
  • ...4 more annotations...
  • if type(other) is type(self):
  • Check types more strictly, like this:
  • is tests for object identity. This means a is b will be True in the case when a and b both hold the reference to the same object
  • __cmp__ was removed from python 3 so avoid it
  •  
    mixin class
Benjamin Bandt-Horn

Classes & Iterators - Dive Into Python 3 - 0 views

  • Comprehensions are just a simple form of iterators. Generators are just a simple form of iterators
  • The first argument of every class method, including the __init__() method, is always a reference to the current instance of the class. By convention, this argument is named self.
  • self is not a reserved word in Python, merely a naming convention
  • ...6 more annotations...
  • To build an iterator from scratch, Fib needs to be a class, not a function.
  • in most cases), __iter__() simply returns self, since this class implements its own __next__() method.
  • a for loop will call this automatically, but you can also call it yourself manually
  • the __next__() method raises a StopIteration exception, this signals to the caller that the iteration is exhausted. Unlike most exceptions, this is not an error; it’s a normal condition that just means that the iterator has no more values to generate. If the caller is a for loop, it will notice this StopIteration exception and gracefully exit the loop. (In other words, it will swallow the exception.)
  • Do not use yield here; that’s a bit of syntactic sugar that only applies when you’re using generators. Here you’re creating your own iterator from scratch; use return instead
  • raise StopIteration
  •  
    Comprehensions are just a simple form of iterators. Generators are just a simple form of iterators
1 - 8 of 8
Showing 20 items per page