Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I'm actually teaching a Python class on OOP and as I'm catching up on the most proficient method to clarify classes, I saw a empty class definition: 

class Employee:

    pass

The model at that point proceeds to characterize a name and different attributes for an object of this class:

john = Employee()

john.full_name = "john doe"

Interesting!!

I'm contemplating whether there's a method to powerfully characterize a function for an occasion of a class this way? something like: 

john.greet() = print 'Hello, World!'

This didn't work in my Python interpreter, however, is there another method of doing it?

1 Answer

0 votes
by (26.4k points)

A class is pretty much an extravagant wrapper for a dict of traits (attributes) to objects. At the point when you start up a class you can allot to its attributes, and those will be stored in the foo.__dict__; similarly, you can glance in foo.__dict__ for any attributes you have just composed. 

This implies you can do some perfect unique things like: 

class Employee: pass

def foo(self): pass

Employee.foo = foo

just as allotting to a specific example

Looking for a good python tutorial course? Join the python certification course and get certified.

For more details, do check out the below video tutorial...

Related questions

0 votes
1 answer
asked Jul 2, 2019 in Python by Sammy (47.6k points)
0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...