Python Forum
Object reference in Dict - not resolved at runtime
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Object reference in Dict - not resolved at runtime
#1
Here is my code:

total_runtime = None
mail_pfx = "dVDS FULL5 CN"

mail_subj = {
	'completed' : f"{mail_pfx} : [INFO] Refresh completed successfully in: {total_runtime}.",
	'warnissues' : f"{mail_pfx} : [ALERT] Refresh completed with issues in: {total_runtime}"
	}

class Stopwatch():

	def __init__(self):
		self.tick = 0
		self.tock = 0
		self.time = 0

	def start(self):
		self.tick = time.perf_counter()

	def stop(self):
		self.tock = time.perf_counter()
		self.time = timedelta(seconds=math.ceil(self.tock-self.tick))

	def __str__(self):
		return str(self.time)

total_runtime = Stopwatch()
total_runtime.start()
total_runtime.stop()

print(mail_subj['warnissues'])
This is printing "dVDS FULL5 CN : [ALERT] Refresh completed with issues in: None"

I want it to resolve the object at runtime instead of compile time, is there any way to do this?
Reply
#2
f-strings are just special ways to create strings. They're not objects. Any variables or expressions inside the string are evaluated at runtime exactly once, not later. Compare:

author = 'Alice'
string1 = author
string2 = f'{author}'

author = 'Bonnie'
print(string1)
print(string2)
The strings assigned to both string1 and string2 are immutable and not re-evaluated later even if author is subsequently modified. Both print statements will produce "Alice". Your string is being evaluated at runtime. It's just happening before all the variables in it are set to what you want to display.

For your code, you could move the dictionary assignment to be after total_runtime is set. Or you could leave the variable out of the dictionary and append it in the print. Something like:

mail_subj = {
    'completed' : f"{mail_pfx} : [INFO] Refresh completed successfully in:",
    'warnissues' : f"{mail_pfx} : [ALERT] Refresh completed with issues in:"
    }
[...]
total_runtime.stop()
print(mail_subj['completed'], total_runtime)
Reply
#3
Thank you kindly!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class and runtime akbarza 4 400 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  how to return a reference to an object? Skaperen 8 1,251 Jun-07-2023, 05:30 PM
Last Post: Skaperen
  painfully slow runtime when handling data dadazhu 3 980 Jan-20-2023, 07:11 PM
Last Post: snippsat
  Big O runtime nested for loop and append yarinsh 4 1,397 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  Reducing runtime memory usage in Cpython interpreter david_the_graower 2 2,228 Oct-18-2021, 09:56 PM
Last Post: david_the_graower
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 4,903 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  Repeating Words in MadLib [resolved] actual_satan 1 1,820 May-07-2021, 08:45 AM
Last Post: ibreeden
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,572 Sep-07-2020, 08:02 AM
Last Post: perfringo
  Sort a dict in dict cherry_cherry 4 76,230 Apr-08-2020, 12:25 PM
Last Post: perfringo
  PyCharm asking for VC++ runtime 14.0 whereas I have already installed VC++ 19.0 SarmadiRizvi 1 1,837 Apr-02-2020, 06:17 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020