DEV Community

loading...
Cover image for Using Python Functions As Classes

Using Python Functions As Classes

abdurrahmaanj profile image Abdur-Rahmaan Janhangeer ・1 min read

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

and a class is like that:

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

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

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()

gives out

4
BWM
moving

Now Let Us Add Attributes

adding attributes to functions

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

testing

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

gives out

dev.to
1123234

More Craziness: Adding Methods To Functions

adding an add method:

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

some info:

alt text

testing

print(devdotto.add(1,2))

gives out

3

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!

Discussion (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 Author

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 Author

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 Author

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 Author

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 Author

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

Forem Open with the Forem app