On Sat, Mar 19, 2011 at 7:57 PM, joy99 <subhakolkata1...@gmail.com> wrote: > > Dear Group, > > I am trying to pose two small questions. > > 1) I am using Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v. > 1500 32 bit (Intel)] on win32 Type "copyright", "credits" or > "license()" for more information, on WINXP SP2. > > As I am writing a code for class like the following: > IDLE 2.6.5 >>>> class Message: > def __init__(self,str1): > self.text="MY NAME" > def printmessage(self): > print self.text > > It works fine as can be seen in the result: >>>> x1=Message(1) >>>> x1.printmessage() > MY NAME > > Now if I open a new window and write the same code value in > printmessage is giving arbitrary or no values. >
Nothing in Python returns arbitrary values. If you are getting values that you don't expect, then you're probably not copying this code exactly. Anyway, printmessage doesn't give you any values at all- it just writes self.text to the standard output. > Am I doing any problem in writing the code? > > 2) Suppose I have a code: > >>>> def hello(): > print "HELLO SIR" > > Can I write another function where I can call the value of this > function or manipulate it? > What do you mean by the value of the function? Python functions are not mathematical functions- they don't evaluate to a value. You have a function, which is an object. You can pass the function object around just like any other variable. >>> def hello() : ... print "HELLO SIR" ... >>> hello <function hello at 0x000000000227E908> >>> hello() HELLO SIR >>> foo = hello >>> foo() HELLO SIR A function can also return something. But that isn't the value of the function because the function has side effects and can potentially do different things even if you call it with the same arguments. >>> x = iter(['a','b','c']) >>> next(x) 'a' >>> next(x) 'b' >>> next(x) 'c' In your case, your function isn't returning anything. It's just writing text to stdout (a side effect as far as the program is concerned) and then returning nothing. In some languages, such as Java, you'd get an error if you tried storing the return value of a function that doesn't return anything. In Python, you just get the None object. > If any learned member can slightly help me out. > > Thanks in advance, > Best Regards, > Subhabrata. > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list