Using Python Functions As Classes

Using Python Functions As Classes

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!

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

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:

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:

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!

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Guide to Python Programming Language

Guide to Python Programming Language