#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Unhappy Python - functions to methods



    hmm i want to make these two functions into methods in my class Ribosome.. but i dont really know how to im new at python/programming

    #class Ribosome(Acid):

    def into_dict():
    global dict_VAR #global variable used in the into disp_val func
    dict_VAR = dict()
    f = open("DNAcodons.txt")
    for i in f.readlines():
    i = i.strip()
    if i[0] == ">":
    conList = i[1:].split(",")
    abbriev = conList[0]
    short_1 = conList[1]
    loong_1 = conList[2]
    short = short_1[1:]
    loong = loong_1[1:]
    else:
    dict_VAR[i] = (abbriev, short, loong)
    return dict_VAR

    def disp_val(value, mode):
    into_dict()
    if mode == 0:
    look = dict_VAR[value]
    return look[0]
    if mode == 1:
    look = dict_VAR[value]
    return look[1] + " "
    else:
    look = dict_VAR[value]
    return look[2]

    print disp_val("GTA", 0)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,902
    Rep Power
    481
    I suggest you work through the python tutorial, and that you clarify your post.

    If possible, please show a small sample of input and output you expect.

    Please post your program using code tags to preserve indentation. Follow the link at my signature for instructions.
    Code:
    class c:
        def method():
            pass
    Indentation is all it takes to make a function into a class method. This particular function isn't very useful.
    Code:
    >>> c()
    <__main__.c instance at 0x2546f80>
    >>> c().m
    <bound method c.m of <__main__.c instance at 0x20f1200>>
    >>> c().m()    #  what is the given argument??????????????   Puzzle you'll need to understand.
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: m() takes no arguments (1 given)
    >>> c.m()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unbound method m() must be called with c instance as first argument (got nothing instead)
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    Just to demonstrate the difference, here is your original code again, using the highlight tags:

    python Code:
     
         #class Ribosome(Acid):
     
    def into_dict():
        global dict_VAR #global variable used in the into disp_val func
        dict_VAR = dict()
        f = open("DNAcodons.txt") 
        for i in f.readlines():
            i = i.strip()
            if i[0] == ">":
                conList = i[1:].split(",")
                abbriev = conList[0]
                short_1 = conList[1]
                loong_1 = conList[2]
                short = short_1[1:]
                loong = loong_1[1:]
            else:
                dict_VAR[i] = (abbriev, short, loong)
        return dict_VAR
     
    def disp_val(value, mode): 
        into_dict()
        if mode == 0:
            look = dict_VAR[value]
            return look[0]
        if mode == 1:
            look = dict_VAR[value]
            return look[1] + " "
        else:
            look = dict_VAR[value]
            return look[2]
     
    print disp_val("GTA", 0)

    As you can see, [highlight] (and the related [code]) tags retain the original formatting of the code, making it readable. This is especially important in Python, where indentation controls the block structure.

    As for converting the functions to methods, well, first off, you need to consider just what you are looking for the class to be. For example, as it is right now, you have the class inheriting from Acid, which presumably is a superclass for the DNA and RNA classes; but a Ribosome isn't an amino acid, it's a biological structure that acts upon amino acids. Thus, from the very start, there's a problem with your class design.

    So, the question you should be considering is this: just what are you simulating, or representing?
    Last edited by Schol-R-LEA; October 29th, 2012 at 11:36 AM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

IMN logo majestic logo threadwatch logo seochat tools logo