hi, and welcome to Python!  my comments below...

> I made a class with the name student that prints simply name of the student
> as well as his roll_no also, pasting code here .

sounds pretty straightforward


> class student:
>     def __init__(self,name,roll_no):
>         self.name=name
>         self.roll_no

i recommend you Capitalize the name of the class "student" to
"Student" so that you can more easily visualize the difference between
a class and other variables and functions, i.e., class Student.

also, this is the end of __init__(). the rest of the code needs to be
"dedented" indicate that it is *not* part of that method body:


>     def display(self):
>         print 'name',self.name,'roll_no',self.roll_no

similarly, your application code is below, and should not be indented at all:

> s1=student("sudhanshu",21) # also change to Student later
> s2=student("rahul",22)
> s1.display()
> s2.display()
>
> student is a class name

exactly why i suggest changing the name to "Student" instead.


> problem 1: Why we always use first method as a __init__

__init__() is not required. in general, people put that in their code
as the 1st method if they have one, and they generally do, but not
always.


> problem 2: why we put first argument as a self inside the __init__ parentheses

self is the required 1st parameter for *all* methods, not just
__init__(). exceptions are static and class methods.


> if __init__ is a constructor then how it works.

Python allocates your objects for you, so it is more of an
"initializer" than a "constructor." it is the 1st method that Python
calls on your behalf after it instantiates your object for you. using
__init__() allows you to "initialize" additional attributes before
your newly-created instance is returned to you.


> How I can run this code , working on ubuntu : but when write inside the
> terminal python filename.py but it shows nothing

if you make the above fixes, then running it on the cmd-line with "$
python FILE.py" will work, but just replace "FILE" with the name of
your file. (also, the "$" is the shell prompt, so don't type that in.)

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to