Silly newbie question - Carrot character (^)

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Nov 6 05:33:25 CET 2010


On Fri, 05 Nov 2010 23:21:11 -0400, Philip Semanchuk wrote:

> Take the OP's question. How is one supposed to find out about bitwise
> operators in Python? AFAICT they're not mentioned in the tutorial, and
> neither are decorators, assert(), global, exec, the ternary if
> statement, etc.

The tutorial isn't meant as an exhaustive lesson on every single Python 
feature. There are plenty of other resources available: learning Python 
*starts* with the python.org tutorial (or some equivalent), it doesn't 
end there.

As far as the OP's question, I'm kind of surprised that he wasn't told 
that Google is his friend. The very first hit for "python caret" answers 
his question. If he had spent even five seconds googling, he would have 
got his answer.

http://www.google.co.uk/search?hl=en&safe=off&q=python+caret&btnG=Search



> It seems that plowing through a document written for language lawyers is
> the only formal way to learn about those language features, and that
> could be improved upon IMO.

Google on "Python book" and you will find dozens of other formal ways to 
learn about language features.

At the interactive interpreter, type 

help("^")

and press Enter, and (assuming your help system is set up correctly, 
which it may not be) you will get a table of operators, including ^ and &.

Is it reasonable to assume somebody knows that symbols like + - * and ^ 
are called "operators" in most programming languages? I think so -- you 
have to assume some level of knowledge. So you could start at the Python 
language reference and scan the table of contents by eye for "operators" 
"operations", "ops" or similar. This gives:

5. Expressions
   5.1. Arithmetic conversions
   5.2. Atoms
   5.3. Primaries
   5.4. The power operator
   5.5. Unary arithmetic and bitwise operations
   5.6. Binary arithmetic operations
   5.7. Shifting operations
   5.8. Binary bitwise operations
   ...


Admittedly, the language reference is a bit n00b-hostile with it's use of 
extended BNF notation for syntax. But I don't think it's entirely 
unreasonable to expect even a newbie to read and understand:

"The ^ operator yields the bitwise XOR (exclusive OR) of its arguments, 
which must be plain or long integers. The arguments are converted to a 
common type."

So, yes, the docs could be improved. They could *always* be improved, 
because somebody will always find something they don't like -- too 
detailed, not detailed enough, too long, too short, too hard for newbies, 
too dumbed down, not dumbed down enough...


-- 
Steven



More information about the Python-list mailing list