Hi all, I'm using pulumi (with python) and need to...
# getting-started
d
Hi all, I'm using pulumi (with python) and need to access an Output property's raw value in order to do a string comparison. Is there any way to accomplish this? Everything I've tried has resulted in the property remaining an object of type Output[T]
m
Hi there — yes, in order to get at the underlying/resolved value, you’ll need to use
apply
as described here: https://www.pulumi.com/docs/intro/concepts/inputs-outputs/?language=python#apply
d
I thought that the result of the call to apply is another Output object?
m
Yes — the result of the call to
apply
is an
Output[T]
, but within the callback that’s passed to apply, you can work with the raw `T`:
Copy code
bucket = s3.Bucket('my-bucket')

def compare(a, b):
    if a == b:
        print("match")
    else:
        print("no match")

bucket.id.apply(lambda id: compare(id, "some-string"))
You can also do something like this, which can be a little nicer to work with:
Copy code
bucket = s3.Bucket('my-bucket')
some_known_value = "foo"

# Capture the result as an Output[bool].
is_match = bucket.id.apply(lambda id: id == some_known_value)

# Use its apply() to work with the result.  
is_match.apply(lambda m: print("Match!" if m else "No match."))
d
Okay thank you! I'll give that a shot