+ 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? 🤔
7 Respostas
+ 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. 😉
+ 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'> 👈
😑
+ 2
Thanks Pluto |^_^| Its works 🤘
https://code.sololearn.com/c1nSDE7uqxk8/?ref=app
+ 1
+ 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.
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)