Results 1 to 3 of 3

Thread: Simple Python class question.

  1. #1
    Associate
    Join Date
    Sep 2003
    Posts
    30

    Simple Python class question.

    Learning/relearning Python. Grotesqeuly simple question:

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

    #! /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.

  2. #2

    Re:Simple Python class question.

    Yep, pretty simple:


    #! /usr/bin/env python

    class A:
    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 A(4).f(4).y

    Returns 5 as expected.

  3. #3
    Associate
    Join Date
    Sep 2003
    Posts
    30

    Re:Simple Python class question.

    Great. Thanks. Now will try and work out why.

Similar Threads

  1. Cannot start X again ....
    By DjiT in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 02-24-2005, 12:51 AM
  2. Still can't get nvidia card to work.
    By gmoreno in forum Linux - General Topics
    Replies: 6
    Last Post: 01-08-2005, 04:00 PM
  3. GL not working ati 7000
    By Mip in forum Linux - Software, Applications & Programming
    Replies: 19
    Last Post: 10-28-2004, 09:29 PM
  4. Resulution problem in KDE
    By Buller in forum Linux - General Topics
    Replies: 3
    Last Post: 08-13-2004, 05:25 PM
  5. fd0 not a block device
    By Pantheus in forum Linux - General Topics
    Replies: 25
    Last Post: 10-08-2002, 10:26 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •