Setting up a class

Discussion in 'Python' started by shaun, Sep 6, 2012.

  1. shaun

    shaun Guest

    Hi all,

    So I'm trying to to OO a script which is currently in place on work. It connects to the database and makes multiple strings and sends them to a server..

    But I'm having major problems since I am new to python I keep trying to do it as I would do it in Java but classes seem to be very different. I was wondering could someone answer a few questions?

    1) Is there anything I should know about passing in variables from another script to the class?

    2) When I'm passing variables back to the script they seem to come back blank as if I haven't done it correctly (I declare the empty variable at the top of the class, I use the information I get from the database to fill it and I send it back) Is there anything I'm not doing right with this.

    3)When I want to use a method from a class in another class method it neverseems to work for me, I have a feeling this is to do with "self" but im not too sure??

    Any help would be appreciated.
    Thanks,
    Shaun
     
    shaun, Sep 6, 2012
    #1
    1. Advertising

  2. On Thu, Sep 6, 2012 at 8:00 AM, shaun <> wrote:
    > Hi all,
    >
    > So I'm trying to to OO a script which is currently in place on work. It connects to the database and makes multiple strings and sends them to a server.
    >
    > But I'm having major problems since I am new to python I keep trying to do it as I would do it in Java but classes seem to be very different. I was wondering could someone answer a few questions?
    >
    > 1) Is there anything I should know about passing in variables from another script to the class?
    >
    > 2) When I'm passing variables back to the script they seem to come back blank as if I haven't done it correctly (I declare the empty variable at thetop of the class, I use the information I get from the database to fill itand I send it back) Is there anything I'm not doing right with this.
    >
    > 3)When I want to use a method from a class in another class method it never seems to work for me, I have a feeling this is to do with "self" but im not too sure??
    >
    > Any help would be appreciated.
    > Thanks,
    > Shaun
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    You should take the smallest snippit of your code that shows your
    problem and copy it here along with traceback where it fails. Java is
    not python and vice versa, so some of your ideas will be confused for
    a while


    --
    Joel Goldstick
     
    Joel Goldstick, Sep 6, 2012
    #2
    1. Advertising

  3. shaun

    MRAB Guest

    On 06/09/2012 13:00, shaun wrote:
    > Hi all,
    >
    > So I'm trying to to OO a script which is currently in place on work. It connects to the database and makes multiple strings and sends them to a server.
    >
    > But I'm having major problems since I am new to python I keep trying to do it as I would do it in Java but classes seem to be very different. I was wondering could someone answer a few questions?
    >
    > 1) Is there anything I should know about passing in variables from another script to the class?
    >
    > 2) When I'm passing variables back to the script they seem to come back blank as if I haven't done it correctly (I declare the empty variable at the top of the class, I use the information I get from the database to fill it and I send it back) Is there anything I'm not doing right with this.
    >
    > 3)When I want to use a method from a class in another class method it never seems to work for me, I have a feeling this is to do with "self" but im not too sure??
    >
    > Any help would be appreciated.
    > Thanks,
    > Shaun
    >

    You should have a look as a Python tutorial to help you get used to the
    language. It shouldn't take you long, provided that you remember that
    Python isn't Java and don't try to write Java in Python. :)

    Here's one you could look at:

    Python for Java programmers
    http://python4java.necaiseweb.org/Main/TableOfContents
     
    MRAB, Sep 6, 2012
    #3
  4. shaun

    Dave Angel Guest

    On 09/06/2012 08:00 AM, shaun wrote:
    > Hi all,
    >
    > So I'm trying to to OO a script which is currently in place on work. It connects to the database and makes multiple strings and sends them to a server.
    >
    > But I'm having major problems since I am new to python I keep trying to do it as I would do it in Java but classes seem to be very different. I was wondering could someone answer a few questions?
    >
    > 1) Is there anything I should know about passing in variables from another script to the class?


    Things don't get passed to a class. They get passed to an initializer
    (and to the constructor, but you've probably never used one of those),
    or to methods of the class. There's only one script in a given program,
    the other source files are modules. If code in one module wants to use
    code or data from another module, there are two general ways to do it.
    One is to use the "from module1 import global1, func2" syntax. In this
    case, they effectively become globals of the current file. And the
    other is to use module1.func2() syntax, where you qualify where to
    find the function.

    As others have pointed out to you, "from xxx import *" is very
    risky, as you make everything global, and the reader can no longer tell
    where a particular symbol comes from. Further, any name collisions are
    silently dealt with, on the assumption that you REALLY know what you're
    doing.

    >
    > 2) When I'm passing variables back to the script they seem to come back blank as if I haven't done it correctly (I declare the empty variable at the top of the class, I use the information I get from the database to fill it and I send it back) Is there anything I'm not doing right with this.

    You don't pass variables back to a script. If you mean return a value
    from a function, then say so. Note that a return statement takes a
    single object, but that object could very well be a tuple. So it's
    perfectly reasonable for a function to return as:

    return first, second

    And the caller might have done something like:
    a, b = myfunc()

    first will go into a,and second will go into b. This subtlety is
    because the expression first,second is a standard way to specify a
    tuple of size 2. And tuple unpacking can be done similarly with his,
    hers = expression. No restriction with size of 2, but you do want the
    same number of items in both places.

    There is no need to declare any variable, anywhere. If it needs an
    initial value, then assign it. Further, things 'at the top of the
    class' are class attributes, not just variables. How can you ask if
    there's anything you're not doing right when you post no code with your
    vague question?

    > 3)When I want to use a method from a class in another class method it never seems to work for me, I have a feeling this is to do with "self" but im not too sure??


    If one class method needs to call another method of the same class,
    you'll usually want to use the self object:
    class ....
    def method1(self):
    arg1 = 42
    self.method2(arg1)
    def method2(self, value);
    ...dosomething interesting...

    Naturally, when it's a method of some other class, or when you need a
    different instance of the same class, then you'd better have an instance
    to use with it.
    someinstance.method2(arg1)



    --

    DaveA
     
    Dave Angel, Sep 6, 2012
    #4
  5. On 06/09/2012 8:20 AM, MRAB wrote:
    > On 06/09/2012 13:00, shaun wrote:
    >> Hi all,
    >>
    >> So I'm trying to to OO a script which is currently in place on work.
    >> It connects to the database and makes multiple strings and sends them
    >> to a server.
    >>
    >> But I'm having major problems since I am new to python I keep trying
    >> to do it as I would do it in Java but classes seem to be very
    >> different. I was wondering could someone answer a few questions?
    >>
    >> 1) Is there anything I should know about passing in variables from
    >> another script to the class?
    >>
    >> 2) When I'm passing variables back to the script they seem to come
    >> back blank as if I haven't done it correctly (I declare the empty
    >> variable at the top of the class, I use the information I get from the
    >> database to fill it and I send it back) Is there anything I'm not
    >> doing right with this.
    >>
    >> 3)When I want to use a method from a class in another class method it
    >> never seems to work for me, I have a feeling this is to do with "self"
    >> but im not too sure??
    >>
    >> Any help would be appreciated.
    >> Thanks,
    >> Shaun
    >>

    > You should have a look as a Python tutorial to help you get used to the
    > language. It shouldn't take you long, provided that you remember that
    > Python isn't Java and don't try to write Java in Python. :)
    >
    > Here's one you could look at:
    >
    > Python for Java programmers
    > http://python4java.necaiseweb.org/Main/TableOfContents
    >

    The link given seems dated and incomplete - see "operator overloading".

    Sorry I can't assist the OP.

    Colin W.
     
    Colin J. Williams, Sep 6, 2012
    #5
  6. On Thu, 6 Sep 2012 05:00:32 -0700 (PDT), shaun
    <> declaimed the following in
    gmane.comp.python.general:

    >
    > But I'm having major problems since I am new to python I keep trying to do it as I would do it in Java but classes seem to be very different. I was wondering could someone answer a few questions?
    >


    First mistake... IGNORE anything you know about Java classes.
    (Including the one-class-per-file).

    > 1) Is there anything I should know about passing in variables from another script to the class?


    Normally you don't pass variables to the "class" -- you create an
    INSTANCE of the class (and may pass some parameters to the __init__()
    method.

    >
    > 2) When I'm passing variables back to the script they seem to come back blank as if I haven't done it correctly (I declare the empty variable at the top of the class, I use the information I get from the database to fill it and I send it back) Is there anything I'm not doing right with this.
    >


    Python doesn't have "declarations" (other than "global" and related
    in newer versions).

    Normally one "return"s data from a method call. Otherwise one access
    instance attributes using instance.attribute notation.

    > 3)When I want to use a method from a class in another class method it never seems to work for me, I have a feeling this is to do with "self" but im not too sure??
    >


    "self" is the instance whose method is being called.

    If you are trying to use a class for something that has no
    instances, forget it, just package everything up as module and import
    the module.

    > Any help would be appreciated.


    Read the language reference manual, and the Python tutorial...
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Sep 6, 2012
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,788
    Thomas Weidenfeller
    Oct 12, 2005
  2. christopher diggins
    Replies:
    16
    Views:
    756
    Pete Becker
    May 4, 2005
  3. Joseph Turian
    Replies:
    5
    Views:
    596
  4. Karsten Wutzke
    Replies:
    0
    Views:
    389
    Karsten Wutzke
    Jun 29, 2007
  5. Marco
    Replies:
    4
    Views:
    136
Loading...

Share This Page