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>
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...
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 2.x2.3
raw_input will wait for the user to enter text and then return the result as a string.
foo = raw_input("Put a message here that asks the user for input")
In the above example foo will store whatever input the user provides.
Python 3.x3.0
input will wait for the user ...
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...
Python 2.x2.3
In Python 2.x, to continue a line with print, end the print statement with a comma. It will automatically add a space.
print "Hello,",
print "World!"
# Hello, World!
Python 3.x3.0
In Python 3.x, the print function has an optional end parameter that is what...
The simplest way to iterate over a file line-by-line:
with open('myfile.txt', 'r') as fp:
for line in fp:
print(line)
readline() allows for more granular control over line-by-line iteration. The example below is equivalent to the one above:
with open('myfile.txt', 'r') as fp:
...
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...
with open('myfile.txt', 'w') as f:
f.write("Line 1")
f.write("Line 2")
f.write("Line 3")
f.write("Line 4")
If you open myfile.txt, you will see that its contents are:
Line 1Line 2Line 3Line 4
Python doesn't automatically add line b...
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 expiry = new Date();
expiry.setTime(expiry.getTime() - 3600);
document.cookie = name + "=; expires=" + expiry.toGMTString() + "; path=/"
This will remove the cookie with a given name.
This a Basic example for using the MVVM model in a windows desktop application, using WPF and C#. The example code implements a simple "user info" dialog.
The View
The XAML
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
...
The following snippet encodes the data stored in d into JSON and stores it in a file (replace filename with the actual name of the file).
import json
d = {
'foo': 'bar',
'alice': 1,
'wonderland': [1, 2, 3]
}
with open(filename, 'w') as f:
json.dump(d, f)
The following snippet opens a JSON encoded file (replace filename with the actual name of the file) and returns the object that is stored in the file.
import json
with open(filename, 'r') as f:
d = json.load(f)