Tutorial by Examples: ble

A normal function declaration looks like this: function foo(){ } A function defined like this is accessible from anywhere within its context by its name. But sometimes it can be useful to treat function references like object references. For example, you can assign an object to a variable based...
Variables can be accessed via dynamic variable names. The name of a variable can be stored in another variable, allowing it to be accessed dynamically. Such variables are known as variable variables. To turn a variable into a variable variable, you put an extra $ put in front of your variable. $va...
Anonymous functions can be assigned to variables for use as parameters where a callback is expected: $uppercase = function($data) { return strtoupper($data); }; $mixedCase = ["Hello", "World"]; $uppercased = array_map($uppercase, $mixedCase); print_r($uppercased); ...
The use construct is used to import variables into the anonymous function's scope: $divisor = 2332; $myfunction = function($number) use ($divisor) { return $number / $divisor; }; echo $myfunction(81620); //Outputs 35 Variables can also be imported by reference: $collection = []; $a...
Setting a specific Seed will create a fixed random-number series: random.seed(5) # Create a fixed state print(random.randrange(0, 10)) # Get a random integer between 0 and 9 # Out: 9 print(random.randrange(0, 10)) # Out: 4 Resetting the seed will create the same &qu...
min, max, and sorted all need the objects to be orderable. To be properly orderable, the class needs to define all of the 6 methods __lt__, __gt__, __ge__, __le__, __ne__ and __eq__: class IntegerContainer(object): def __init__(self, value): self.value = value def __rep...
In Python, variables inside functions are considered local if and only if they appear in the left side of an assignment statement, or some other binding occurrence; otherwise such a binding is looked up in enclosing functions, up to the global scope. This is true even if the assignment statement is ...
There are several special variable types that a class can use for more easily sharing data. Instance variables, preceded by @. They are useful if you want to use the same variable in different methods. class Person def initialize(name, age) my_age = age # local variable, will be destroyed ...
We have three methods: attr_reader: used to allow reading the variable outside the class. attr_writer: used to allow modifying the variable outside the class. attr_accessor: combines both methods. class Cat attr_reader :age # you can read the age but you can never change it attr_writer...
There is a problem when using optional arguments with a mutable default type (described in Defining a function with optional arguments), which can potentially lead to unexpected behaviour. Explanation This problem arises because a function's default arguments are initialised once, at the point whe...
<table> <tr> <th>Heading 1/Column 1</th> <th>Heading 2/Column 2</th> </tr> <tr> <td>Row 1 Data Column 1</td> <td>Row 1 Data Column 2</td> </tr> <tr> <td>Row 2 Data Column...
Create a new file called hello.sh with the following content and give it executable permissions with chmod +x hello.sh. Execute/Run via: ./hello.sh #!/usr/bin/env bash # Note that spaces cannot be used around the `=` assignment operator whom_variable="World" # Use printf to sa...
class adder(object): def __init__(self, first): self.first = first # a(...) def __call__(self, second): return self.first + second add2 = adder(2) add2(1) # 3 add2(2) # 4
Extensions can contain functions and computed/constant get variables. They are in the format extension ExtensionOf { //new functions and get-variables } To reference the instance of the extended object, self can be used, just as it could be used To create an extension of String that adds ...
For example, you can take the absolute value of each element: list(map(abs, (1, -1, 2, -2, 3, -3))) # the call to `list` is unnecessary in 2.x # Out: [1, 1, 2, 2, 3, 3] Anonymous function also support for mapping a list: map(lambda x:x*2, [1, 2, 3, 4, 5]) # Out: [2, 4, 6, 8, 10] or convert...
For example calculating the average of each i-th element of multiple iterables: def average(*args): return float(sum(args)) / len(args) # cast to float - only mandatory for python 2.x measurement1 = [100, 111, 99, 97] measurement2 = [102, 117, 91, 102] measurement3 = [104, 102, 95, 101] ...
A basic Employees table, containing an ID, and the employee's first and last name along with their phone number can be created using CREATE TABLE Employees( Id int identity(1,1) primary key not null, FName varchar(20) not null, LName varchar(20) not null, PhoneNumber varchar(10)...
Let's say we have a query of the remaining horsemen that needs to populate a Person class. NameBornResidenceDaniel Dennett1942United States of AmericaSam Harris1967United States of AmericaRichard Dawkins1941United Kingdom public class Person { public string Name { get; set; } public int...
When the Repeater is Bound, for each item in the data, a new table row will be added. <asp:Repeater ID="repeaterID" runat="server" OnItemDataBound="repeaterID_ItemDataBound"> <HeaderTemplate> <table> <thead> ...
Instance variables are unique for each instance, while class variables are shared by all instances. class C: x = 2 # class variable def __init__(self, y): self.y = y # instance variable C.x # 2 C.y # AttributeError: type object 'C' has no attribute 'y' c1 = C(3) c1....

Page 2 of 62