Creating Class in Python

class Employee:
    #these are the instance variables
    sal=0;
    name="none";

    #This is a function definition.
        #the keyword "self" is MUST!It is similar to "this" in java.It says that this method belongs to Employee class.(explicitly saying)
    #"__init__() is the constructor in python.

    def __init__(self,name,sal):
        self.name=name;
        self.sal=sal;
        print "instance created";

    #a normal method
    def display(self):
        print "*"*20;
        print "Name:",self.name;
        print "Salary:",self.sal;
        print "*"*20;

#class ends

#creating class instance and storing in variable "temp_var"
temp_var=Employee("max",10000);
#display the values
temp_var.display();

Output:

instance created
********************
Name: max
Salary: 10000
********************

About PraveenMax
My Interests: ------------------ Programming , Drawing & Music

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: