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. Python and PHP question.
    By Jdun in forum Linux - Software, Applications & Programming
    Replies: 7
    Last Post: 11-26-2004, 02:41 PM
  2. (Hopefully) Simple question regarding samba
    By elovkoff in forum Linux - Hardware, Networking & Security
    Replies: 9
    Last Post: 10-28-2002, 06:21 PM
  3. Simple Question
    By CP in forum General Chat
    Replies: 47
    Last Post: 09-16-2002, 10:09 PM
  4. Simple Question
    By Baldy in forum Linux - Software, Applications & Programming
    Replies: 5
    Last Post: 07-26-2002, 03:39 PM
  5. simple add user question
    By pinehead in forum Linux - General Topics
    Replies: 6
    Last Post: 04-30-2002, 03:06 AM

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
  •