Some Minor questions on Class and Functions
joy99
subhakolkata1234 at gmail.com
Sun Mar 20 09:04:11 EDT 2011
On Mar 20, 11:13 am, joy99 <subhakolkata1... at gmail.com> wrote:
> On Mar 20, 9:39 am, Steven D'Aprano <steve
>
>
>
> +comp.lang.pyt... at pearwood.info> wrote:
> > On Sat, 19 Mar 2011 16:57:58 -0700, joy99 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.
>
> > The description of your problem does not make sense to me. Can you show
> > an example?
>
> > > 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?
>
> > No. The string "HELLO SIR" is a local variable to the hello() function.
> > You cannot modify it from outside that function. Since your hello()
> > function prints the result, instead of returning it, another function
> > cannot capture it either.
>
> > Perhaps what you want is something like this:
>
> > def hello(message="HELLO SIR"):
> > return message
>
> > Now you can call the function, and print the result:
>
> > print hello()
>
> > If you want to capture the return value, you can:
>
> > result = hello()
> > print result.lower()
>
> > If you want to change the message used, you can pass it to the function
> > as an argument:
>
> > hello("Greetings and salutations!")
>
> > Hope this helps,
>
> > --
> > Steven
>
> Thanks Steven and Benjamin for your kind time to answer my question. I
> am sending the code soon, actual code is pretty long that has so many
> variables, it may well take your long time to evaluate, so I am making
> a sizable prototype and trying to send it to you.
> Best Regards,
> Subhabrata.
Sir,
I am writing the code as below. I am trying to send also the error
messages and my doubts.
class Message:
def __init__(self,string1,string2,lenstr1,lenstr2):
self.string1="MY"
self.string2="NAME"
self.lenstr1=lenstr1
self.lenstr2=lenstr2
def lenstring(self):
lenstr1=len(self.string1)
lenstr2=len(self.string2)
def printstr(self):
print lenstr1
print lenstr2
IDLE 2.6.5
>>> ================================ RESTART ================================
>>>
>>> x1=Message(1,2,3,4)
>>> x1.printstr()
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
x1.printstr()
File "D:/Python26/Message3.py", line 11, in printstr
print lenstr1
NameError: global name 'lenstr1' is not defined
My doubts are:
i) Am I doing something wrong? In calling the values/parameters?
ii)As I am calling the class with so many parameters, I must be doing
something very wrong. What is the solution if you can kindly suggest.
Best Regards,
Subhabrata.
More information about the Python-list
mailing list