Skip to main content

Home/ Mac Attack/ Group items tagged value

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. An Informal Introduction to Python - Python v2.7.6 documentation - 0 views

  •  
    Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. List items need not all have the same type.
Benjamin Bandt-Horn

Function overloading in Python: Missing - Stack Overflow - 0 views

  • keyword arguments with default values can go a long way.
  • In Python, I think it's more accepted to use duck typing -- asking what an object can do, rather than what it is
  • it goes against the spirit of Python to worry a lot about what types are passed into methods
  •  
    keyword arguments with default values can go a long way.
Benjamin Bandt-Horn

Ternary conditional operator in Python - Stack Overflow - 0 views

  •  
    Yes, it was added in version 2.5. It's frowned upon by some pythonistas, so keep that in mind. The syntax is: a if test else b First test is evaluated, then either a or b is returned based on the Boolean value of test; if test evaluates to True a is returned, else b is returned. For example: >>> 'true' if True else 'false' 'true' >>> 'true' if False else 'false' 'false'
Benjamin Bandt-Horn

python - *args and **kwargs? - Stack Overflow - 0 views

  •  
    One case where *args and **kwargs are useful is when writing wrapper functions (such as decorators) that need to be able accept arbitrary arguments to pass through to the function being wrapped. For example, a simple decorator that prints the arguments and return value of the function being wrapped: def mydecorator( f ):   @functools.wraps( f )      def wrapper( *args, **kwargs ): ...
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

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 - 7 of 7
Showing 20 items per page