Skip to main content

Mojo keywords reference

Keywords are reserved identifiers with special meaning in the language. They cannot be used as ordinary variable names unless escaped with backticks.

Core language keywords

KeywordCategoryPurposeExample
andLogicalBoolean AND operatorif x > 0 and y > 0:
orLogicalBoolean OR operatorif valid or debug:
notLogicalBoolean NOT operatorif not found:
ifControl flowConditional executionif condition:
elifControl flowAdditional condition in if statementelif other_condition:
elseControl flowDefault branch in conditionals or loopselse:
forControl flowIteration loopfor item in items:
whileControl flowConditional loopwhile count < 10:
breakControl flowExit innermost loopbreak
continueControl flowSkip to next loop iterationcontinue
returnControl flowReturn from functionreturn value
passControl flowNo-op placeholder statementpass
raiseExceptionRaise an exceptionraise ValueError("error")
tryExceptionBegin exception handling blocktry:
exceptExceptionException handler clauseexcept e:
finallyExceptionAlways-execute block in try statementfinally:
withContextContext manager statementwith open("file") as f:
asBindingImport aliasing and exception bindingimport math as m
fromImportSelective importfrom math import pi
importImportModule importimport collections
inMembershipMembership test operatorif x in list:
isIdentityIdentity test operatorif x is None:

Mojo-specific keywords

KeywordPurposeExample
fnFunction declaration (non-raising by default)fn compute() -> Int:
defFunction declaration (can raise)def process():
structStructure type declarationstruct Point:
traitTrait interface declarationtrait Comparable:
comptimeCompile-time evaluationcomptime SIZE = 1024
varVariable/field declarationvar x: Int
refReference argument conventionfn modify(ref x: Int):

Type introspection keywords

KeywordPurposeExample
origin_ofReturns origin (lifetime) of referenceorigin_of(x)
type_ofReturns type of expression at compile timetype_of(x)
conforms_toTests trait conformance at compile timeconforms_to(T, Trait)

Literal keywords

KeywordTypeValue
TrueBoolBoolean true value
FalseBoolBoolean false value
NoneNoneTypeAbsence of value
SelfType referenceReference to enclosing struct or trait type
_DiscardDiscard pattern in destructuring

Escaped keywords

Use backticks to use keywords as identifiers:

`fn` = 42          # Valid - escaped keyword as variable name
`struct` = "text"  # Valid - escaped keyword

obj.fn()           # Valid - keywords after dot don't need escaping
point.struct       # Valid - member access doesn't require escaping

Case sensitivity

All Mojo keywords are case-sensitive:

True               # Keyword - boolean literal
true               # Not a keyword - would be treated as identifier

None               # Keyword - None literal
none               # Not a keyword - would be treated as identifier

Self               # Keyword - type reference
self               # Not a keyword - conventional name for instance argument

Was this page helpful?