+ 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
7 Antwoorden
+ 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
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