Some Minor questions on Class and Functions

J

joy99

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.5def __init__(self,str1):
self.text="MY NAME"
def printmessage(self):
print self.text

It works fine as can be seen in the result:MY NAME

Now if I open a new window and write the same code value in
printmessage is giving arbitrary or no values.

Am I doing any problem in writing the code?

2) Suppose I have a code:
print "HELLO SIR"

Can I write another function where I can call the value of this
function or manipulate it?

If any learned member can slightly help me out.

Thanks in advance,
Best Regards,
Subhabrata.
 
B

Benjamin Kaplan

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       def __init__(self,str1):
               self.text="MY NAME"
       def printmessage(self):
               print self.text

It works fine as can be seen in the result: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:

       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..... print "HELLO SIR"
....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.
 
S

Steven D'Aprano

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 def __init__(self,str1):
self.text="MY NAME"
def printmessage(self):
print self.text

It works fine as can be seen in the result: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:

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,
 
J

joy99

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
   def __init__(self,str1):
           self.text="MY NAME"
   def printmessage(self):
           print self.text
It works fine as can be seen in the result:
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:
   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,

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.
 
J

joy99

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?
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,

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
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.
 
N

Noah Hall

class Message:
   def __init__(self,string1,string2,lenstr1,lenstr2):
       self.string1="MY"
       self.string2="NAME"
       self.lenstr1=lenstr1
       self.lenstr2=lenstr2

The variables string1 and string2 that you're passing here are in fact
useless. They don't do anything inside the method. Is there any point
for them? There's no need to pass __init__ variables just so you can
assign self.variable_name = "fixed text".
   def lenstring(self):
       lenstr1=len(self.string1)
       lenstr2=len(self.string2)

I think you want self.lenstr1 and self.lenstr2 here - otherwise you're
assigning local variables with the same name as those defined within
the class, and throwing them away.
   def printstr(self):
       print lenstr1
       print lenstr2

Again, I think you want self.lenstr1 and self.lenstr2
 
J

joy99

The variables string1 and string2 that you're passing here are in fact
useless. They don't do anything inside the method. Is there any point
for them? There's no need to pass __init__ variables just so you can
assign self.variable_name = "fixed text".


I think you want self.lenstr1 and self.lenstr2 here - otherwise you're
assigning local variables with the same name as those defined within
the class, and throwing them away.


Again, I think you want self.lenstr1 and self.lenstr2

Sir,
Thanks for your kind time and suggestion. I'll repair.
Best Regards,
Subhabrata.
 
J

joy99

The variables string1 and string2 that you're passing here are in fact
useless. They don't do anything inside the method. Is there any point
for them? There's no need to pass __init__ variables just so you can
assign self.variable_name = "fixed text".


I think you want self.lenstr1 and self.lenstr2 here - otherwise you're
assigning local variables with the same name as those defined within
the class, and throwing them away.


Again, I think you want self.lenstr1 and self.lenstr2

Sir,
It worked. Just Thanks.
Best Regards,
Subhabrata.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top