DEV Community

Cover image for Using Python Functions As Classes
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer

Posted on

Using Python Functions As Classes

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 Python Function

a function is like that



def devdotto():
    pass


Enter fullscreen mode Exit fullscreen mode

and a class is like that:



class Car:
    def __init__(self):
        self.wheel = 4
        self.make = 'BWM' # i know

    def move(self):
        print('moving')


Enter fullscreen mode Exit fullscreen mode

more info:

alt text

make and wheels are attributes
while move is a method

some tests



car = Car()

print(car.wheel)
print(car.make)

car.move()


Enter fullscreen mode Exit fullscreen mode

gives out



4
BWM
moving


Enter fullscreen mode Exit fullscreen mode

Now Let Us Add Attributes

adding attributes to functions



devdotto.name = 'dev.to'
devdotto.users = 1123234


Enter fullscreen mode Exit fullscreen mode

testing



print(devdotto.name)
print(devdotto.users)


Enter fullscreen mode Exit fullscreen mode

gives out



dev.to
1123234


Enter fullscreen mode Exit fullscreen mode

More Craziness: Adding Methods To Functions

adding an add method:



devdotto.add = lambda x,y: x+y


Enter fullscreen mode Exit fullscreen mode

some info:

alt text

testing



print(devdotto.add(1,2))


Enter fullscreen mode Exit fullscreen mode

gives out



3

Enter fullscreen mode Exit fullscreen mode




Conclusion

Since some one liner libs make use of IEF (Immediately Executed Functions) using lambdas to compile even loops, these can be useful, but i can't think of any use cases (for using functions as classes), this said, this is one of py's party tricks, hence the cover image.

img credit: Photo by Jason Leung on Unsplash

Any ideas? Comment them out below!

Top comments (13)

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