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!

python

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...

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 Hello World Program - Create & Run Your First Python Program in PyCharm

Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.

Standard Data Types in Python - Python Tutorial - Python Training

This video on 'Standard Data Types in Python' will help you establish a foothold on Python by helping you learn basic concepts.