How to translate this duck-typing (Python) to Java generics? - java

Consider the following simple Python function by way of example:
def quantize(data, nlevels, quantizer=lambda x, d: int(floor(x/d))):
llim = min(data)
delta = (max(data) - llim)/(nlevels - 1) # last level x == max(data) only
y = type(data)
if delta == 0:
return y([0] * len(data))
else:
return y([quantizer(x - llim, delta) for x in data])
And here it is in action:
>>> from random import random
>>> data = [10*random() for _ in range(10)]
>>> data
[6.6181668777075018, 9.0511321773967737, 1.8967672216187881, 7.3396890304913951,
4.0566699095012835, 2.3589022034131069, 0.76888247730320769, 8.994874996737197,
7.1717500363578246, 2.887112256757157]
>>> quantize(data, nlevels=5)
[2, 4, 0, 3, 1, 0, 0, 3, 3, 1]
>>> quantize(tuple(data), nlevels=5)
(2, 4, 0, 3, 1, 0, 0, 3, 3, 1)
>>> from math import floor
>>> quantize(data, nlevels=5, quantizer=lambda x, d: (floor(x/d) + 0.5))
[2.5, 4.5, 0.5, 3.5, 1.5, 0.5, 0.5, 3.5, 3.5, 1.5]
This function certainly has flaws --for one thing, it does not validate arguments, and it should be smarter about how it sets the type of the returned value--, but it has the virtue that it will work whether the elements in data are integers or floats or some other numeric type. Also, by default it returns a list of ints, though, by passing a suitable function as the optional quantizer argument, this type can be changed to something else. Furthermore, if the data parameter is a list, the returned value will be a list; if data is a tuple, the returned value will be a tuple. (This last feature is certainly the weakest one, but it is also the one that I'm least interested in replicating in Java, so I did not bother to make it more robust.)
I would like to write an efficient Java-equivalent of this function, which means figuring out how to get around Java's typing. Since I learned Java (aeons ago), generics were introduced into the language. I've tried learning about Java generics, but find them pretty incomprehensible. I don't know is this is due to early-onset senility, or because of the sheer growth in Java's complexity since I last programmed in it (ca. 2001), but every page I find on this topic is more confusing than the previous one. I'd really appreciate it if someone could show me how to do this in Java.
Thanks!

One solution to the input/output type question might be to use the Number class and its subclasses along with wildcards. If you wanted to accept any type of numerical argument, you could either specify the input type to be Number OR ? extends Number. If the input is a list, the latter form has an advantage as it will ensure that each element of the list is of the same type (which must be a subclass of Number). The ? is known as a Wildcard, and when it is expressed as ? extends Number it is a "Bounded Wildcard", and may only refer to a subtype of the bounding type.
Example:
public List<Number> func(List<? extends Number> data, Number nlevels)
This would take a List of a specific subclass of Number, a Number for the nlevels parameter, and return a List of Numbers
As for the function input parameter, it would be possible to input a Method, though the type checking at this point gets difficult, as you will be passing data of a bounded unknown parameter to a Method object. I am not exactly sure how that would work.
As for the return type, it would be possible to specify another parameter, a class object (likely a ? extends Number again) that the list elements would be cast (or converted) to.
public List<? extends Number> quantize(List<? extends Number> data,
Number nlevels,
Method quantizer,
Class<? extends Number> returnType)
That is an attempt at a possible declaration for your function in Java. The implementation, however, is somewhat more complex.

It's not quite what you asked, but may I suggest trying out Jython? You'd be able to take your Python code and compile directly to Java bytecode. As you haven't used Java since 2001 and you seem to be using Python these days, you may find Jython much easier to work with than having to bring yourself up to speed on all the changes in Java beforehand.

Related

Apache Spark reduce action calling function more times than expected [duplicate]

I am running Spark-1.4.0 pre-built for Hadoop-2.4 (in local mode) to calculate the sum of squares of a DoubleRDD. My Scala code looks like
sc.parallelize(Array(2., 3.)).fold(0.0)((p, v) => p+v*v)
And it gave a surprising result 97.0.
This is quite counter-intuitive compared to the Scala version of fold
Array(2., 3.).fold(0.0)((p, v) => p+v*v)
which gives the expected answer 13.0.
It seems quite likely that I have made some tricky mistakes in the code due to a lack of understanding. I have read about how the function used in RDD.fold() should be communicative otherwise the result may depend on partitions and etc. So example, if I change the number of partitions to 1,
sc.parallelize(Array(2., 3.), 1).fold(0.0)((p, v) => p+v*v)
the code will give me 169.0 on my machine!
Can someone explain what exactly is happening here?
Well, it is actually pretty well explained by the official documentation:
Aggregate the elements of each partition, and then the results for all the partitions, using a given associative and commutative function and a neutral "zero value". The function op(t1, t2) is allowed to modify t1 and return it as its result value to avoid object allocation; however, it should not modify t2.
This behaves somewhat differently from fold operations implemented for non-distributed collections in functional languages like Scala. This fold operation may be applied to partitions individually, and then fold those results into the final result, rather than apply the fold to each element sequentially in some defined ordering. For functions that are not commutative, the result may differ from that of a fold applied to a non-distributed collection.
To illustrate what is going on lets try to simulate what is going on step by step:
val rdd = sc.parallelize(Array(2., 3.))
val byPartition = rdd.mapPartitions(
iter => Array(iter.fold(0.0)((p, v) => (p + v * v))).toIterator).collect()
It gives us something similar to this Array[Double] = Array(0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 9.0) and
byPartition.reduce((p, v) => (p + v * v))
returns 97
Important thing to note is that results can differ from run to run depending on an order in which partitions are combined.

converting a Haskell function into java "function"

I have this function in Haskell, and I am wondering how it can be converted to Java, especially using streams:
build = [(w,m,n,g) | w <- [240..1280], m <- [2,4..20], n <- [2..100], g <- [240..1280], ((w - 2*m - n*g) `mod` (n+1) == 0), n*g+2*m <= w, n*g <= w]
(I'm not a Haskell expert, but I know enough to be dangerous.)
The example code given has several Haskell constructs that map reasonably
well into Java constructs:
A Haskell list is lazy, so it corresponds to a Java Stream.
The ranges used are of integers, so they correspond to IntStream.
For example, [240..1280] corresponds to IntStream.rangeClosed(240, 1280).
A range with a step has no direct correspondence in Java, but it can easily
be computed; you just have to do a bit of arithmetic and then map the values
from the sequential range to the one with steps. For example, [2, 4..20]
can be written as
IntStream.rangeClosed(1, 10).map(i -> 2 * i)
A condition on a list comprehension corresponds to filtering a stream through
a predicate.
A comprehension with multiple generators corresponds to flatmapping
of nested streams.
There is no general way to represent tuples in Java. Various third party
libraries provide tuple implementations with varying tradeoffs regarding
generics and boxing. Or, you can just write your own class with the fields
you want. (This can be quite tedious if you use lots of different kinds of
tuples, though.) In this case, the tuple is simply four ints, so it's easily
represented using an int array with four elements.
Putting it all together, we get the following.
static Stream<int[]> build() {
return IntStream.rangeClosed(240, 1280).boxed()
.flatMap(w -> IntStream.rangeClosed(1, 10).map(m -> 2 * m).boxed()
.flatMap(m -> IntStream.rangeClosed(2, 100).boxed()
.flatMap(n -> IntStream.rangeClosed(240, 1280)
.filter(g -> ((w - 2*m - n*g) % (n+1) == 0))
.filter(g -> n*g+2*m <= w)
.filter(g -> n*g <= w)
.mapToObj(g -> new int[] { w, m, n, g }))));
}
This is clearly quite verbose compared to the original Haskell, but you can easily see where the Haskell constructs have ended up in the Java code. I believe this is correct, as it seems to generate the same output as the Haskell code.
Note that we are generating values using IntStream but we want the flatmap to give a stream of arrays (which are objects), whereas IntStream.flatMap returns an IntStream. Perhaps ideally there would be a flatMapToObj operation, but there isn't, so we must box the int value into an Integer object and then call Stream.flatMap it.
One could assign the stream pipeline to a variable of type Stream, but this wouldn't be very convenient, as Java streams can be used at most once. Since constructing such a stream is cheap (compared to evaluating it) it's reasonable to write a function build() that returns a freshly created stream, ready to be evaluated by the caller.
When the following Java code is run,
System.out.println(build().count());
System.out.println(build().findFirst().map(Arrays::toString).orElse("not found"));
System.out.println(build().reduce((a, b) -> b).map(Arrays::toString).orElse("not found"));
the result is:
654559
[484, 2, 2, 240]
[1280, 20, 5, 248]
Running the following Haskell code (the definition of build is copied from the question)
build = [(w,m,n,g) | w <- [240..1280], m <- [2,4..20], n <- [2..100], g <- [240..1280],
((w - 2*m - n*g) `mod` (n+1) == 0), n*g+2*m <= w, n*g <= w]
main = do
print (length build)
print (head build)
print (last build)
gives the following output:
654559
(484,2,2,240)
(1280,20,5,248)
So the transliteration appears correct to my eye.
Times for the head (in Java, findFirst) and last (in Java, reduce((a, b) -> b)) operations were as follows: (updated using GHC 7.6.3 -O2)
head last
GHC 8s 36s
JDK 3s 9s
This at least shows that both systems provide laziness, as the computation is short-circuited after the first element is found, whereas finding the last element requires all to be computed.
Interestingly, in Haskell, calling all three of length, head, and last doesn't take any more time than just calling last (around 36s) presumably because of memoization. There's no memoization in Java, but of course you could explicitly store the results in an array or List and process that multiple times.
Overall, though, I was startled at how much faster the Java implementation is. I don't really understand Haskell performance, so I'll leave it to Haskell experts to comment on that. It's quite possible I've done something wrong, though mostly I just copied the function from the question into a file and compiled it using GHC.
My environment:
JDK 9, GHC 7.6.3 -O2, MacBook Pro mid 2014 2-core 3GHz Intel Core i7

Why - in Java 1.8 - is Function<V,R> used and not Function<R,V>?

The order seems odd because in regular Java the return type is always specified first. As in:
public static double sum(Iterable<Number> nums) { ... }
Why then, in the Function and BiFunction classes has the choice been made to specify them the other way around? As in:
interface Function<T,R>
interface BiFunction<T,U,R>
I'm not asking here for opinions as to which is better, but specifically:
a) Is there any technical or other (non-stylistic) benefit in preferring one order over the other? Or is it an arbitrary choice?
b) Is anyone aware of any documented explanation, or any stated reason from an authoritative source, why one was chosen over the other?
Aside: the order seems even more odd if extended to higher arities. For example, a hypothetical QuadFunction:
interface QuadFunction<A,B,C,D,R> { ... }
(At the time of writing the highest arity in the library is 2 - i.e. BiFunction.)
See: http://download.java.net/jdk8/docs/api/java/util/function/package-summary.html
It is to be consistent with prior existing notation.
The mathematical integer division function extended into the rational numbers:
(\): I x I -> Q
Functional programming version of the above (like Haskell, Ocaml)
division :: Integer -> (Integer -> Rational)
or
division :: Integer -> Integer -> Rational
All three say "the division function takes two integers and returns a rational number". It is backwards, in a functional paradigm, to say your returns first. C has taught us to say "we return a rational number in the division function, which takes two integers" (ex float division(int a, int b){}).
In Java, your return type is on the left of methods because Java wants to look like C. The designers of C thought "int main(int argv, char *argv[])" looked better than "main(int argv, char *argv[]) int". When writing code, atleast for me, I more often than not know what a method will return before I know what it will need. (edit 1: and we write lines like String s=removeSpaces(textLine), so the return on the left matches the variable on the left)
In C#, func looks the same way as the Java 8 Function.
My guess is that it's more intuitive for method chaining which might be a typical use case for lambdas, i.e.
IntStream.range(1, 10).map(Ints::random).filter(x -> x % 2 == 0)
So, method sequense here reads left to right and lambdas go left to right. So why not having the type params go left to right?
Escalating this a bit further - the reason might be that the English language reads left to right. :-)
UPDATE
I was very surprised to find out that this is something which takes place for maths modern arabic notation:
Latin complex numbers
Arabic complex numbers
In this example arabic notation in every char mirrors latin. One can track this by the angle sign and i (imagenary unit) char - in both cases it has a dot. In the linked wiki article there is also an example of a reversed lim arrow (compared to Java 8 lamda's arrow direction). This could mean that arabic Java, if it was ever developed, would look a bit differently. :-)
Disclaimer: I have background in maths, but I had no idea of the arabic notation when I was answering this question.
In ordinary procedural and OO programming, functions/methods generally take a list of parameters and return some result:
int max(int num1, int num2)
When rewriting function signatures as callback-based (such as for parallel or asynchronous processing), it has been a longstanding practice to convert the signature by appending the return callback as the last parameter:
void maxAsync(int num1, int num2, Callback<int> callback) // pseudo-Java
A current example of this pattern can be found in GWT RPC processing.
This style originated in the Lisp style of languages with the so-called continuation-passing style, where functions are chained by passing a function to a function as a parameter. Since in Lisp arguments are evaluated left-to-right, the function that's consuming the values needs to be at the end of the list. This arrangement has been adopted by imperative languages for continuity and because it's been traditional to tack on additional optional parameters (boolean flags and the like) at the end of the parameter list.
It is the explicit intent to make it more convenient to program in a functional style in Java. Now, in mathematics, a function is generally written like
f: A -> B
(i.e., a function from As to Bs). This corresponds also to the notation in functional languages, Scala and already existing functional libraries for Java.
In other words: it is just the right thing.
Note that a functional interface is not a method and a method is not a functional interface, hence it is not clear what the syntax of the former has to do with the latter.
Just my opinion: to look the same way as Function in guava does. Having the order the other way around would cause a lot of confusion I guess.
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Function.html

Generating sequentially all combination of a finite set using lexicographic order and bitwise arithmetic

Consider all combination of length 3 of the following array of integer {1,2,3}.
I would like to traverse all combination of length 3 using the following algorithm from wikipedia
// find next k-combination
bool next_combination(unsigned long& x) // assume x has form x'01^a10^b in binary
{
unsigned long u = x & -x; // extract rightmost bit 1; u = 0'00^a10^b
unsigned long v = u + x; // set last non-trailing bit 0, and clear to the right; v=x'10^a00^b
if (v==0) // then overflow in v, or x==0
return false; // signal that next k-combination cannot be represented
x = v +(((v^x)/u)>>2); // v^x = 0'11^a10^b, (v^x)/u = 0'0^b1^{a+2}, and x ← x'100^b1^a
return true; // successful completion
}
What should be my starting value for this algorithm for all combination of {1,2,3}?
When I get the output of the algorithm, how do I recover the combination?
I've try the following direct adaptation, but I'm new to bitwise arithmetic and I can't tell if this is correct.
// find next k-combination, Java
int next_combination(int x)
{
int u = x & -x;
int v = u + x;
if (v==0)
return v;
x = v +(((v^x)/u)>>2);
return x;
}
I found a class that exactly solve this problem. See the class CombinationGenerator here
https://bitbucket.org/rayortigas/everyhand-java/src/9e5f1d7bd9ca/src/Combinatorics.java
To recover a combination do
for(Long combination : combinationIterator(10,3))
toCombination(toPermutation(combination);
Thanks everybody for your input.
I have written a class to handle common functions for working with the binomial coefficient, which is the type of problem that your problem falls under. It performs the following tasks:
Outputs all the K-indexes in a nice format for any N choose K to a file. The K-indexes can be substituted with more descriptive strings or letters. This method makes solving this type of problem quite trivial.
Converts the K-indexes to the proper index of an entry in the sorted binomial coefficient table. This technique is much faster than older published techniques that rely on iteration. It does this by using a mathematical property inherent in Pascal's Triangle. My paper talks about this. I believe I am the first to discover and publish this technique, but I could be wrong.
Converts the index in a sorted binomial coefficient table to the corresponding K-indexes. I believe it might be faster than the link you have found.
Uses Mark Dominus method to calculate the binomial coefficient, which is much less likely to overflow and works with larger numbers.
The class is written in .NET C# and provides a way to manage the objects related to the problem (if any) by using a generic list. The constructor of this class takes a bool value called InitTable that when true will create a generic list to hold the objects to be managed. If this value is false, then it will not create the table. The table does not need to be created in order to perform the 4 above methods. Accessor methods are provided to access the table.
There is an associated test class which shows how to use the class and its methods. It has been extensively tested with 2 cases and there are no known bugs.
To read about this class and download the code, see Tablizing The Binomial Coeffieicent.
It should not be hard to convert this class to Java.

Programming in Python vs. programming in Java

I've been writing Java for the last couple of years , and now I've started to write in python (in addition).
The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java.
Any tips on how to escape this pattern of "Writing Java in Python"?
Thanks!
You might consider immersing yourself in the Python paradigms. The best way is to first know what they are then explore the best practices by reading some literature and reviewing some code samples. I recommend Learning Python by Mark Lutz; great for beginners and advanced users.
You'll do yourself a great injustice if you program with Python and fail to leverage all of the built-in, developer-friendly, Pythonic syntax.
As my French teacher used to say, "French isn't just English with different words."
If you are new to Python and coming from Java (or C#, or other similar statically typed OO language), these classic articles from PJ Eby and Ryan Tomayko are necessary reading:
Python Is Not Java (PJE)
Java is not Python, either (PJE)
Python Interfaces are not Java Interfaces (PJE)
The Static Method Thing (Tomayko)
Getters/Setters/Fuxors (Tomayko)
You could start by reading The Zen of Python. It'll give you some insight into how Python code is supposed to be written, provided you understand the language enough to understand what it's talking about. :-)
Some of the major ways in which Python differs from C/Java-like languages are:
List comprehensions.
Support for functional programming.
The use of certain Pythonic constructs instead of similar C-like constructs although both seem to work (list comprehensions can be argued to be a part of this, but there are others).
There are others, but these are the main ones that bugged me when I first started Python (and I had come from years of Java like you).
Before using any of these, it is helpful to understand why you should go for pythonic code rather than the usual C/Java way in Python, although both give you the same output.
For starters, Python provides some powerful features not available in C/Java that makes your code much clearer and simpler (although this is subjective, and might not look any better to someone coming from Java at first). The first two points fall into this category.
For example, support for functions as first class objects and closures makes it easy to do things that would need all kinds of weird acrobatics with inner classes in Java.
But a major reason is that Python is an interpreted language, and certain constructs are much faster than the equivalent C/Java-like code. For example, list comprehensions are usually a lot faster than an equivalent for-loop that iterates over the indices of a list and accesses each item by index. This is a very objective benefit, and IMHO a lot of the "Python in way too slow" way of thinking comes from using Java-style code shoe-horned into Python.
One of the best ways to learn about pythonic code is to read other people's code. I actually learnt a lot by looking at Python code posted in answers to SO questions. These often come with explanations and it is usually obvious why it is better than non-pythonic code (speed, clarity, etc.).
Edit:
Of course, there are other ways of getting other people's code. You can also download and look through the code of any good open source Python project. Books are also a good resource, I would recommend O'Reilly Python Cookbook. It has lots of useful code examples and very detailed explanations.
1) Python supports many (but not all) aspects of
object-oriented programming; but it is
possible to write a Python program without
making any use of OO concepts.
1) Java supports only object-oriented
programming.
2) Python is designed to be used interpretively.
A Python statement may be entered at the
interpreter prompt
(>>>)
, and will be executed
immediately. (Implementations make some
use of automatic compilation into bytecodes
(.pyc files).
2) Programs written in Java must be explicitly
compiled into bytecodes (.class files),
though an IDE may do this automatically in a
way that is transparent to the user. Java does
not support direct execution of statements -
though there are tools like Dr. Java that
support this.
3) Python is dynamically typed:
• A variable is introduced by assigning a
value to it. Example:
someVariable = 42
• A variable that has been assigned a value of
a given type may later be assigned a value
of a different type. Example:
someVariable = 42
someVariable = 'Hello, world'
3) Java is
statically typed
:
• A variable must be explicitly declared to be
of some type before assigning a value to it,
though declaration and assignment may be
done at the same time. Examples:
int someVariable;
int someVariable = 42;
• A variable that has been declared to be of a
particular type may not be assigned a value
of a different type.
4) Python supports the following built-in data
types:
Plain integers (normally 32-bit integers in
the range -2147483648 through
2147483647).
• Long integers (size limited only by memory
size of the machine running on)
• Booleans (False and True).
• Real numbers.
• Complex numbers.
In addition, Python supports a number of
types that represent a collection of values -
including strings, lists, and dictionaries.
4) Java has two kinds of data types: primitive
types and reference types. Java supports the
following primitive data types:
• byte - 8-bit integers
• short - 16-bit integers
• int - 32-bit integers
• long - 64-bit integers (Java also supports a
class java.math.BigInteger to represent
integers whose size is limited only by
memory)
• float - 32-bit real numbers.
• double - 32-bit real numbers.
• boolean - (false and true).
• char - a single character.
In addition, Java supports arrays of any type
as the reference types, and the API includes
the class String and a large number of classes
used for collections of values.
5)
Python is line-oriented:
statements end at the
end of a line unless the line break is explicitly
escaped with . There is no way to put more
than one statement on a single line.
Examples:
this is a statement
this is another statement
this is a long statement that extends over more \
than one line
5)
Statements in Java always end with a
semicolon (;)
. It is possible for a statement to
run over more than one line, or to have
multiple statements on a single line.
Examples:
this is a statement;
this is another statement;
this is a long statement that extends over more
than one line;
a statement; another; another;
6)
Python comments begin with #
and extend to
the end of the line. Example:
This is a comment
A new statement starts here
6) Java has two kinds of comments. A comment
beginning with // extend to the end of the
line (like Python comments). Comments can
also begin with /* and end with */. These
can extend over multiple lines or be
embedded within a single line. Examples:
// This is a comment
A new statement starts here
/* This is also a comment */
/* And this is also a comment, which is
long enough to require several lines
to say it. */
Statement starts /* comment */ then continues
7) Python strings can be enclosed in either single
or double quotes (' or ""). A character is
represented by a string of length 1. Examples:
'This is a string'
"This is also a string" # Equivalent
'c' # A string
"c" # An equivalent string
Python uses the following operators for
constructing compound boolean expressions:
and, or and not. Example:
not(x > 0 and y > 0) or z > 0
7) Java strings must be enclosed in double
quotes (""). A character is a different type of
object and is enclosed in single quotes (').
Examples:
"This is a String"
'c' // A character, but not a String
Java uses the following operators for
constructing compound boolean expressions:
&&, ||, ! and ^ (meaning exclusive or)
Example:
! (x > 0 && y > 0) || z > 0 ^ w > 0
8) In Python, the comparison operators
(>, <, >=, <=, == and !=) can be applied to numbers
,
strings, and other types of objects), and
compare values in some appropriate way (e.g.
numeric order, lexical order) where possible.
8) In Java, most of the comparison operators
( >, <, >=, and <=) can be applied only to
primitive types. Two (== and !=) can be
applied to any object, but when applied to
reference types they test for same (different)
object rather than same (different) value.
9) There is no universally-accepted Python
convention for naming classes, variables,
functions etc.
9) By convention, most names in Java use mixed
case. Class names begin with an uppercase
letter; variable and function names begin with
a lowercase letter. Class constants are named
using all uppercase letters with underscores.
Examples:
AClassName
aVariableName
aFunctionName()
A_CLASS_CONSTANT
10) Python definite looping statements have the
form for variable in expression: Example:
for p in pixels:
something
10) Java has two kinds of definite looping
statements. One has the form
for (variable in collection) Example:
for (p in pixels)
something;
11) Python uses the built-in function range() with
for to loop over a range of integers.
Examples:
for i in range(1, 10)
something
(i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9)
for i in range(1, 10, 2)
something
(i takes on values 1, 3, 5, 7, 9)
11) Java uses a different form of the for to loop
over a range of integers. Examples:
for (int i = 1; i < 10; i ++)
something;
(i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9)
for (int i = 1; i < 10; i += 2)
something;
(i takes on values 1, 3, 5, 7, 9)
12) Python conditional statements have the form
if condition: and an optional else part has the
form else:. The form elif condition: is
allowed as an alternative to an else:
immediately followed by an if. Examples:
if x < 0:
something
if x < 0:
something
else:
something different
if x < 0:
something
elif x > 0:
something different
else:
yet another thing
12) Java conditional statements have the form
if (condition) and an optional else part has
the form else (no colon) There is no elif
form - else if is used directly. Examples:
if (x < 0)
something;
if (x < 0)
something;
else
something different;
if (x < 0)
something;
else if (x > 0)
something different;
else
yet another thing;
13) The scope of a Python conditional or looping
statement is denoted by indentation. (If
multiple lines are to be included, care must be
used to be sure every line is indented
identically). Examples:
if x < 0:
do something
do another thing regardless of the value of x
if x < 0:
do something
do something else
do yet a third thing
do another thing regardless of the value of x
13) The scope of a Java conditional or looping
statement is normally just the next statement.
Indentation is ignored by the compiler
(though stylistically it is still highly desirable
for the benefit of a human reader). If
multiple lines are to be included, the scope
must be delimited by curly braces ({ , }).
(Optionally, these can be used even if the
scope is a single line.) Examples:
if (x < 0)
do something;
do another thing regardless of the value of x;
if (x < 0)
do something; // Bad style-don't do this!
do another thing regardless of the value of x;
if (x < 0)
{
do something;
do something else;
do yet a third thing;
}
do another thing regardless of the value of x;
if (x < 0)
{
do something;
}
do another thing regardless of the value of x;
If you want to see some fairly idiomatic Python that does non-trivial stuff, there's Dive Into Python, although Dive Into Python 3 is newer and might be a better source of style tips. If you're looking more for some points to review, there's Code Like a Pythonista.
You could post your code at Refactor my code to see if someone can show you a more Pythonic way to do it.
Definitely not a panacea but I think you should try some code golf in Python. Obviously nobody should write "golfed" code IRL, but finding the most terse way to express something really forces you to exploit the built in functionality of the language.
Someone provided me with this list of how "Python is not Java" when I started Python after Java, and it was very helpful.
Also, check out this similar SO question that I posted a short time ago when in a similar position.
Try to find algorithms that you understand well and see how they are implemented in python standard libraries.
Persist. :)
Learn a few other languages. It will help you make the difference between algorithms (the structure of processing, unchanged between languages) and the local syntaxic features of the language. Then you can "write Foo in Bar" for any combination of languages "Foo" and "Bar".
Eat Python, Sleep Python and Drink Python. That is the only way........
This is useful if you want to understand how to code to python in a more pythonic or correct way: http://www.python.org/dev/peps/pep-0008/

Categories

Resources