tags:

views:

514

answers:

4

I often see something like that: something.property|escape


something is an object, property is it's string property. escape - i don't know :)
What does this mean? And what min python version it is used in?


EDIT: The question was asked wrongly, it said "What does | mean in Python", so the bitwise or answers are correct, but irrelevant, please do not downvote them

A: 

It's a bitwise "or". It means escape if the property doesn't exist/is null.

Joel Coehoorn
Paul, the logical "or" operator is written "or". "|" is bitwise
rodbv
This is incorrect. -1.
Triptych
+7  A: 

obj.property|escape is the way to apply the escape filter in a template, which will HTML escape the string representation of that property.

Vinko Vrsalovic
so, something.property|escape == escape(something.property) ?
Hmm - glad this answer helped, but technically this isn't python.
Triptych
True, my mindreading-fu is powerful.
Vinko Vrsalovic
Dima, only in a template. And equal only in the semantic sense, I'm not sure (may be possible, because my Django is rusty) you can actually write it like "escape(something.property)".
Vinko Vrsalovic
Please be more careful when editing the question — a lot of the answers to the original question ("…mean in Python") are getting downvoted because they no longer look relevant.
Ben Blank
No, you can't rewrite it in a function-calling style in a Django template. You must use the pipe. Django templates != Python code. And you can't just use any Python function this way, it has to be registered as a template filter in a template library loaded in that template.
Carl Meyer
@Ben Blank: You're right. Late, but fixed.
Vinko Vrsalovic
A: 

| is the bitwise "or" operator, most commonly used, these days, to combine options (though that isn't particularly "pythonic"). An example:

# Creates a dialog with "Yes" and "No" buttons in a fictional widget set.
result = Widgets.Dialog("Ex-parrot?", Widgets.YES | Widgets.NO)

It is sometimes confused with ||, which is the logical "or" operator in other popular languages, and is often used to provide an alternative or default for a value which may be missing or unset (this is considered "pythonic"). However, the logical "or" operator in python is simply or. Example:

# Uses a default value if one isn't provided on the command line
x = sys.argv[1] or 0
Ben Blank
+5  A: 

The pipe character indicates that you want to send the results of the left hand side to the filter defined on the right side. The filter will modify the value in some way.

The 'escape' filter is just one of many.

The list of built in filters can be found here: Django Documentation - Built-in filters reference

In a django template the | character definitely does not mean the 'bitwise OR' operator.

bskinner
+1 for the first correct answer
Jonas