screeps-starter-python
1.0.0
1.0.0
  • Introduction
  • Logistics
    • Setup
    • Upgrading Transcrypt
  • Screeps API Usage
    • Constants
    • Optional Arguments
    • Objects - Basic Interaction
    • Objects - Creation and the Keyword "new"
  • Features
    • Console Commands
  • Lodash
    • Introduction
    • As Arguments to API Methods
  • Syntax Changes
    • Summary
    • in Opperator
    • List Behavior
    • del Operator
    • dict Iteration
Powered by GitBook
On this page
  • Transcrypt vs. Python: iterating dicts
  • 1) manually force things to be dictionaries
  • 2) use JS-style or lodash access

Was this helpful?

  1. Syntax Changes

dict Iteration

Transcrypt vs. Python: iterating dicts

In Python, dictionaries are a special case - they have a different class than Object which allows for [] access to values, and iteration methods like .keys(), .values() and .items().

In JavaScript, all objects are "dictionaries". Screeps-transcrypt has made the choice to by default keep all dictionaries created in Python regular JS objects for consistency between JS-created dicts and Python-created dicts.

While this is good for performance, it's not great for pythonic code. .keys(), .values() and .items() are not available in Transcrypt-python.

Instead, there are two possibilities:

1) manually force things to be dictionaries

dict() method does exist, and will turn a regular object into a dictionary with right methods.

my_stuff = dict({
    "a": b,
})

2) use JS-style or lodash access

In JavaScript, lodash methods are often used for object iteration.

Instead of:

for key, value in obj.items():
    print(key, value)
for key in obj.keys():
    print(key)
for value in obj.values():
    print(value)

You can use:

for key, value in _.pairs(obj):
    print(key, value)
for key in Object.keys(obj): # or _.keys(obj)
    print(key)
for value in Object.values(obj): # or _.values(obj)
    print(value)
Previousdel Operator

Last updated 4 years ago

Was this helpful?