Tutorial by Examples: c

The increment and decrement operators exist in prefix and postfix form. int a = 1; int b = 1; int tmp = 0; tmp = ++a; /* increments a by one, and returns new value; a == 2, tmp == 2 */ tmp = a++; /* increments a by one, but returns old value; a == 3, tmp == 2 */ tmp = --b; ...
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...
Closures (also known as blocks or lambdas) are pieces of code which can be stored and passed around within your program. let sayHi = { print("Hello") } // The type of sayHi is "() -> ()", aka "() -> Void" sayHi() // prints "Hello" Like other fun...
Functions may accept closures (or other functions) as parameters: func foo(value: Double, block: () -> Void) { ... } func foo(value: Double, block: Int -> Int) { ... } func foo(value: Double, block: (Int, Int) -> String) { ... } Trailing closure syntax If a function's last parameter ...
class MyClass { func sayHi() { print("Hello") } deinit { print("Goodbye") } } When a closure captures a reference type (a class instance), it holds a strong reference by default: let closure: () -> Void do { let obj = MyClass() // Captures a strong re...
A for loop iterates over a sequence, so altering this sequence inside the loop could lead to unexpected results (especially when adding or removing elements): alist = [0, 1, 2] for index, value in enumerate(alist): alist.pop(index) print(alist) # Out: [1] Note: list.pop() is being used t...
You can define a new class using the class keyword. class MyClass end Once defined, you can create a new instance using the .new method somevar = MyClass.new # => #<MyClass:0x007fe2b8aa4a18>
A class can have only one constructor, that is a method called initialize. The method is automatically invoked when a new instance of the class is created. class Customer def initialize(name) @name = name.capitalize end end sarah = Customer.new('sarah') sarah.name #=> 'Sarah' ...
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...
Ruby has three access levels. They are public, private and protected. Methods that follow the private or protected keywords are defined as such. Methods that come before these are implicitly public methods. Public Methods A public method should describe the behavior of the object being created. T...
In Python 2.6 and higher, math.copysign(x, y) returns x with the sign of y. The returned value is always a float. Python 2.x2.6 math.copysign(-2, 3) # 2.0 math.copysign(3, -3) # -3.0 math.copysign(4, 14.2) # 4.0 math.copysign(1, -0.0) # -1.0, on a platform which supports signed zero ...
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...
Python 3.x3.0 In Python 3, print functionality is in the form of a function: print("This string will be displayed in the output") # This string will be displayed in the output print("You can print \n escape characters too.") # You can print escape characters too. Pyth...
def input_number(msg, err_msg=None): while True: try: return float(raw_input(msg)) except ValueError: if err_msg is not None: print(err_msg) def input_number(msg, err_msg=None): while True: try: return ...
The preferred method of file i/o is to use the with keyword. This will ensure the file handle is closed once the reading or writing has been completed. with open('myfile.txt') as in_file: content = in_file.read() print(content) or, to handle closing the file manually, you can forgo with...
In the event that geolocation fails, your callback function will receive a PositionError object. The object will include an attribute named code that will have a value of 1, 2, or 3. Each of these numbers signifies a different kind of error; the getErrorCode() function below takes the PositionError....
with open(input_file, 'r') as in_file, open(output_file, 'w') as out_file: for line in in_file: out_file.write(line) Using the shutil module: import shutil shutil.copyfile(src, dst)
The following variables set up the below example: var COOKIE_NAME = "Example Cookie"; /* The cookie's name. */ var COOKIE_VALUE = "Hello, world!"; /* The cookie's value. */ var COOKIE_PATH = "/foo/bar"; /* The cookie's path. */ var COOKIE_EXPIRES; ...
var name = name + "=", cookie_array = document.cookie.split(';'), cookie_value; for(var i=0;i<cookie_array.length;i++) { var cookie=cookie_array[i]; while(cookie.charAt(0)==' ') cookie = cookie.substring(1,cookie.length); if(cookie.indexOf(name)==0) ...

Page 22 of 826