How to return variable names with values while calling an object? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to return variable names with values while calling an object?

I'm trying to get an output something like this. >>>import os >>>os.get_terminal_size() # Output: 👇 os.get_terminal_size(columns=80, lines=40) ● How to show these two (columns, lines) with my own class object when I call it, if I define a class like: class MyOS: def __init__(self): self.columns = 80 self.lines = 40 def get_terminal_size(self): return self.columns, self.lines >>> myos = MyOS() >>> myos.get_terminal_size() # Output I get: 👇 😞 (80, 40) # Output I want: 👇😍 myos.get_terminal_size(columns=80, lines=40) # Can't get my head around how to do it? 🤔

21st Mar 2020, 5:21 PM
OR!ON 🛡️
OR!ON 🛡️ - avatar
6 Answers
+ 3
I think they are using named tuple here. example: from collections import namedtuple k=namedtuple("terminal_size", ["height", "width"]) h= k(7,6) print(h) # output: terminal_size(height=7, width=6) print(h.width) # output: 6 print(type(h)) # output: "<class 'terminal_size'>" # should do the work. 😉
1st Apr 2020, 3:53 PM
Ishmam
Ishmam - avatar
+ 2
👑 Prometheus 🇸🇬 Check the types! >>> import os >>> size = os.get_terminal_size() >>> size os.terminal_size(columns=42, lines=23) >>> type(size) <class 'os.terminal_size'> 👈 >>> size2 = f"myos.get_terminal_size(column=80, lines=40)" >>> size2 'myos.get_terminal_size(column=80, lines=40)' >>> type(size2) <class 'str'> 👈 😑
24th Mar 2020, 7:13 AM
OR!ON 🛡️
OR!ON 🛡️ - avatar
6th Apr 2020, 10:01 AM
OR!ON 🛡️
OR!ON 🛡️ - avatar
+ 1
👑 Prometheus 🇸🇬 David Ashton Cépagrave VcC Do you know how to do this? 🤔
24th Mar 2020, 6:57 AM
OR!ON 🛡️
OR!ON 🛡️ - avatar
+ 1
return f"myos.get_terminal_size(columns={self.columns}, lines={self.lines})" I guess this is what you do if you literally want to display that stuff.
24th Mar 2020, 7:07 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
0
Nope I want this as OUTPUT but with my class when I call myos.get_terminal_size() function 👇👇👇 myos.get_terminal_size(columns=80, lines=40)
24th Mar 2020, 6:49 AM
OR!ON 🛡️
OR!ON 🛡️ - avatar