简体   繁体   中英

Symbol vs Operator in Python

I'm reviewing for a test over some basic Python syntax stuff and I'm wanting to make sure I have a proper understanding of the difference between a symbol and an operator. A symbol can be a string of characters or a operator and an operator can only be something that does something to characters or strings right?

A symbol in a programming language is either a binding to some value (eg. variable identifiers), a value itself (eg. "foo" , 123 , True ), keyword (eg. def , class , import , try , except ,...) or other language specific construct( () , {} , [] ,...). So a symbol does not always have to be a string of characters.

In contrast, an operator defines a specific function among one or more values. (There are unary, binary, tertiary,... operators) eg. + in 1+1 , < in a<b are operators

It's noteworthy if you are considering this idea in a compiler's standpoint, everything you write in your code is a symbol. That is even +, - , *, / , are mere symbols to a lexical analyzer. (I assume that this fact is out of the scope of your question). Hence we will restrict our answer to the domain of language syntax.

However this idea is universal for any programming language

Operator is a syntactic representation for some important Python function. For example, and infix + operator as in a + b . There is a module called operator to represent standard operators as functions. Also, special methods (as in the hus787 comment above) can override operators for instances of a class.

Symbol is an element of Python grammar . Symbol can represent a whole program, a statement, operator, name, literal, etc, even indent and dedent (in case of Python).

This terminology is not Python specific even.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM