r/Python • u/BEST_RAPPER_ALIVE • Jun 25 '22
Beginner Showcase I started learning Python a few days ago. I haven't used type() much, so instead I came up with a ridiculous one-liner that lets you check the class type of an object.
I wrote this because I'm obsessed with writing code that looks like English. Obviously I didn't know that you could just do something like
\>>>type(123) is int
True
Here's the code.
_is = lambda data: {'a': lambda _class: 'Yes' if str(type(data)).split("'")[1] == _class else 'No'}
print(_is('asdf')['a']('str')) #Yes
print(_is(12345)['a']('str')) #No
print(_is(12345)['a']('int')) #Yes
print(_is('hello world!')['a']('int')) #No
print(_is([1,2,3,4,5])['a']('list')) #Yes
print(_is(69)['a']('list')) #No
print(_is(True)['a']('bool')) #Yes
print(_is(420)['a']('bool')) #No
print(_is((1,2,3,4,5))['a']('tuple')) #Yes
print(_is("it's morbin' time")['a']('tuple')) #No
print(_is({'a': 1, 'b': 2, 'c': 3})['a']('dict')) #Yes
print(_is('💩')['a']('dict')) #No
8
Jun 26 '22 edited Jun 26 '22
I wrote this because I'm obsessed with writing code that looks like English.
While we’re having Fun:
class Is:
def __init__(self, noun):
self.noun = noun
self.result = {}
def __call__(self, indefinite_article):
return indefinite_article(self)
def __getitem__(self, key):
if key != "?":
raise SyntaxError("Is noun A type must be a question, silly.")
if key not in self.result:
raise SyntaxError("Improperly formed question.")
return print("Yes" if self.result[key] is True else "No")
class A:
def __init__(self, verb):
self.verb = verb
def __call__(self, noun_type):
self.verb.result["?"] = isinstance(self.verb.noun, noun_type)
return self.verb
An = A
Is("asdf")(A)(str)["?"] # Yes
Is("asdf")(An)(int)["?"] # No
Is(3)(A)(str)["?"] # No
Is(3)(An)(int)["?"] # Yes4
u/ekydfejj Jun 25 '22
I wrote this because I'm obsessed with writing code that looks like English.
Enjoy your journey my ocd friend, be careful not to drive yourself crazy, and folks that read your code. But clearly it proved an interesting experiment.
3
u/trasnsposed_thistle Jun 26 '22
I wrote this because I'm obsessed with writing code that looks like English.
yet = who(_writes('English')['like']('this?'))
1
2
u/hhc97 Python Enthusiast Jun 26 '22 edited Jun 26 '22
While we’re on the topic of things we shouldn’t do…
I wrote a program that can turn that entire script into one line:
(lambda _is: [print(_is('asdf')['a']('str')), [print(_is(12345)['a']('str')), [print(_is(12345)['a']('int')), [print(_is('hello world!')['a']('int')), [print(_is([1, 2, 3, 4, 5])['a']('list')), [print(_is(69)['a']('list')), [print(_is(True)['a']('bool')), [print(_is(420)['a']('bool')), [print(_is((1, 2, 3, 4, 5))['a']('tuple')), [print(_is("it's morbin' time")['a']('tuple')), [print(_is({'a': 1, 'b': 2, 'c': 3})['a']('dict')), print(_is('💩')['a']('dict'))][-1]][-1]][-1]][-1]][-1]][-1]][-1]][-1]][-1]][-1]][-1])(lambda data: {'a': lambda _class: 'Yes' if str(type(data)).split("'")[1] == _class else 'No'})Running it:
>>> (lambda _is: [print(_is('asdf')['a']('str')), [print(_is(12345)['a']('str')), [print(_is(12345)['a']('in
t')), [print(_is('hello world!')['a']('int')), [print(_is([1, 2, 3, 4, 5])['a']('list')), [print(_is(69)['a'
]('list')), [print(_is(True)['a']('bool')), [print(_is(420)['a']('bool')), [print(_is((1, 2, 3, 4, 5))['a'](
'tuple')), [print(_is("it's morbin' time")['a']('tuple')), [print(_is({'a': 1, 'b': 2, 'c': 3})['a']('dict')
), print(_is('💩')['a']('dict'))][-1]][-1]][-1]][-1]][-1]][-1]][-1]][-1]][-1]][-1]][-1])(lambda data: {'a':
lambda _class: 'Yes' if str(type(data)).split("'")[1] == _class else 'No'})
Yes
No
Yes
No
Yes
No
Yes
No
Yes
No
Yes
No
>>> 1
1
u/Griffin_Sullivan Jun 25 '22
This is already built in with isinstance(). See https://www.w3schools.com/python/ref_func_isinstance.asp
0
Jun 25 '22 edited Jun 25 '22
Standard python solution
If type variable is boolean: // do stuff
Your version:
If is variable a boolean
Or exactly
if is variable look up a boolean
Which is what I read.
In think the standard python method is nearer to english, because it puts the verb in the right position. I am just missing the of in the sentence. Personally I use if variable.__class__ is bool.
But it is cool, that you did that. It is a great way to learn.
15
u/nathanjell Jun 25 '22
The better answer here is
isinstance, which as a built-in handles direct, indirect, and virtual subclasses as well. It's quite hard to follow what's going on with your dictionary structure. I'd also much prefer to receive a boolean rather than a string of either "Yes" or "No". This is not to disparage your work, but just some constructive feedback!