-3

How can I check if 'ksize' in config = {'median': {}, 'gauss': {'ksize' :( 3,3), 'sigmaX': 3}} is a tuple? Is there any option to get values only from 'ksize' and not from all the gauss values? I am looking for a universal function if I need to check different values in different config values in the future.

rayryeng
  • 102,964
  • 22
  • 184
  • 193

2 Answers2

2
if type(config["gauss"]["ksize"]) is tuple:
  print("It is a tuple")

use the type built in function in python.

print(config["gauss"]["ksize"])prints the tuple(3,3)

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Srishruthik Alle
  • 500
  • 3
  • 14
  • `isinstance()` is python's preferred method to test whether an object is of a specific type – Christoph Rackwitz Dec 26 '20 at 02:57
  • ah I see. Thx for letting me too cause I thought we had to use that `type`. `isIntance()` seems to be more initutive and easy – Srishruthik Alle Dec 26 '20 at 03:02
  • the advantage of `isinstance` is that it respects inheritance of classes. often just the behavior of an object counts, so the tested object is allowed to be of a subclass. if I needed to know specifically if an object is of a class and not of a subclass, I would use `type(obj)` too. – Christoph Rackwitz Dec 26 '20 at 03:04
1
config = {'median': {}, 'gauss': {'ksize' :( 3,3), 'sigmaX': 3}}
print(isinstance(config["gauss"]["ksize"],tuple))

Output

True

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Sagun Devkota
  • 495
  • 3
  • 10