LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2003, 04:30 AM   #1
lugoteehalt
Senior Member
 
Registered: Sep 2003
Location: UK
Distribution: Debian
Posts: 1,215
Blog Entries: 2

Rep: Reputation: 49
Python how name variable inside a class - very simple?


Learning/relearning Python. Grotesquely simple question:

How do I get hold of the value of y from outside this class?

Code:
#! /usr/bin/python

# Try to work out how classes are called.
# classUnderstand.py

class C:
          def __init__(self, x):
                  print "init called", self, x
          def f(self, x):
                  print "f called", self, x
                  y = x + 1
....I want to put something like:
print C(4).f(4).y
anticipating it will print the number 5. It never will.

How do I name the value of y? Does the class need changing somehow?

Thanks any help.
 
Old 10-21-2003, 04:53 AM   #2
abrb220
Member
 
Registered: Jul 2003
Location: Roodepoort South Africa
Distribution: Redhat / Suse
Posts: 39

Rep: Reputation: 15
Also new to python but I got it to work :
----------------------------------------------
#! /usr/bin/python
# Try to work out how classes are called.
# classUnderstand.py
class C:
def __init__(self, x):
print "init called", self, x
def f(self, x):
print "f called", self, x
self.y = x + 1
return self

print C(4).f(4).y
---------------------------------------------
What do you think?
 
Old 10-21-2003, 04:55 AM   #3
abrb220
Member
 
Registered: Jul 2003
Location: Roodepoort South Africa
Distribution: Redhat / Suse
Posts: 39

Rep: Reputation: 15
omg the Indentation got screwed up.Sorry.
 
Old 10-21-2003, 05:06 AM   #4
lugoteehalt
Senior Member
 
Registered: Sep 2003
Location: UK
Distribution: Debian
Posts: 1,215

Original Poster
Blog Entries: 2

Rep: Reputation: 49
Amazing! I'll have to try and work out what it all means. Perhaps was assuming the problem had to do with classes when it really had to do with 'return'. Maybe you have to use it in functions as well. Thanks.
 
Old 10-21-2003, 06:08 AM   #5
abrb220
Member
 
Registered: Jul 2003
Location: Roodepoort South Africa
Distribution: Redhat / Suse
Posts: 39

Rep: Reputation: 15
The only reason to "return self" is to be able to make the call :
C(4).f(4).y

If you do this :

a = C(4) # create instance of class
a.f(4) # call func f
print a.y

You do not need the "return self"
 
Old 10-22-2003, 05:11 AM   #6
lugoteehalt
Senior Member
 
Registered: Sep 2003
Location: UK
Distribution: Debian
Posts: 1,215

Original Poster
Blog Entries: 2

Rep: Reputation: 49
Right, that's the bit I was after, now I've thought about it. Find it a little counter-intuitive. Thanks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipe inside variable isn't working in bash Reginald0 Linux - General 6 12-09-2015 09:56 AM
Defining a non-inline function inside class definition (tm) Programming 6 05-11-2005 09:14 AM
pthreads inside a class maldini1010 Programming 2 02-16-2005 03:01 PM
problem on dynamic function call inside a class! antony_csf Programming 0 06-29-2004 10:15 PM
Class Arrays in Python rootyard Programming 1 02-13-2004 10:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration