DEV Community

Cover image for Using Python Functions As Classes

Using Python Functions As Classes

Abdur-Rahmaan Janhangeer on February 15, 2019

In Python, you can use functions as classes. In py, everything is an object. How? I'm no py expert. Here's how we do it! An Innocent Pyth...
Collapse
 
dmerejkowsky profile image
Dimitri Merejkowsky

Nice one :)

By the way you can turn classes into functions too:

class Foo:
    def __call__(self, a):
        print(f"Calling with a=", a)

foo = Foo()
foo(42)
Collapse
 
vberlier profile image
Valentin Berlier

By the way you can do this:

In [1]: return_hello = lambda *args, **kwargs: 'hello'                   

In [2]: class Foo(metaclass=return_hello): ...                           

In [3]: Foo                                                              
Out[3]: 'hello'
Collapse
 
abdurrahmaanj profile image
Abdur-Rahmaan Janhangeer

Great! That's the needed comment to complement this article!

Collapse
 
stealthmusic profile image
Jan Wedel • Edited

I‘ve written a Python interpreter in Java once where I learned that really everything is an object in python.

I usually ask in job interviews „what’s the difference between Java and Python“ and most candidates say „Java is object-oriented and Python is a scripting language“ :)

Collapse
 
abdurrahmaanj profile image
Abdur-Rahmaan Janhangeer

you are in language making? really awesome!

Collapse
 
stealthmusic profile image
Jan Wedel

Oh no, I only wrote an interpreter that executed precompiled Python code! I did this once as a PoC to run Python on an embedded device. It worked but unfortunately as you might imagine, interpreting Python Byte code in Java which was also interpreted byte code on a slow embedded device was terrible slow.

The point is: I learned how beautifully Python is designed internally.
As you stated: everything’s an object

  • numbers, strings
  • methods
  • classes
  • modules
  • code blocks

This is truly amazing!

Thread Thread
 
abdurrahmaanj profile image
Abdur-Rahmaan Janhangeer

hum ok, embedded programming is another level for me _

Collapse
 
ondrejs profile image
Ondrej

I don't know Abdur.
I understand that you can utilize OOP principles in Python, but if I really want to do OOP AND the scripting lvl I would rather choose Ruby as my language to go. I just hate Python OOP syntax and things associated with the OOP paradigm in Python. Of course, at the end it will work, but I'm really happy to write my scripts without overhead of using OOP in this particular language.
Thanks for the post though, it was good & inspirational :)

Collapse
 
abdurrahmaanj profile image
Abdur-Rahmaan Janhangeer

ruby i agree is less code, more expressive, but i love the mamothy python

Collapse
 
yorodm profile image
Yoandy Rodriguez Martinez

Yep, functions are objects and you can add methods to them. But why would you do that?

Collapse
 
abdurrahmaanj profile image
Abdur-Rahmaan Janhangeer

to learn more about py i guess

Collapse
 
yorodm profile image
Yoandy Rodriguez Martinez

Yeap, that's the coolest reason of all!!

Collapse
 
alialdaw profile image
alialdaw

can i make a command button with a class