Thread: code reuse?

Page 2 of 2 First 12
  • Jump to page:
    #16
  1. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by pylon
    Ok, I did what you suggested with replacing "./" with "/"
    and the result:

    Something's awry.
    I thought if you wanted to create a new file/folder, you could just tell it to write to that directory/file and it would create it if it didn't already exist.

    You don't have to rush it, its not like it'll be the end of the world if I don't get it, but I'd like to see a simple example sometime
    I also took care of the 'self' problem.
    Also, one more of those __ thingys, what's __init__ ?
    Preceding a path with a / means the root directory on *nix systems, I'm not sure how Windows handles this but it's probably not a good idea to use it. Instead you should remove the / and ./ characters from your filenames entirely.

    This means that the line file=open("/element/"+ename+".txt",'w') will become file=open("element/"+ename+".txt",'w').

    Note: that it isn't a good idea to use the names of Pythons built-in functions for your own variables.

    __init__ is the method that is called when you create an instance of an object, so it allows you to set default values for instance variables. Basically this is used to set up or "initialize" the instance .

    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Better would be to pass parameters in when Element() is created:
    Code:
    class Element(object):
        def __init__(self, symbol, name, weight):
            self.symbol = symbol
            self.name = name
            self.weight = weight
    
    pb = Element("pb", "lead", 60)  # or whatever the weight is ;-)
    
    # or
    symbol = raw_input("Enter the symbol:")
    name = ...
    weight = ...
    
    elem = Element(symbol, name, weight)
    -T.
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    88
    Rep Power
    11
    Yes!
    Thankyou very much, you don't understand how much that explained to me righ there!!!
    THankyou thankyou thankyou!!!!
    You'll get a rep. bonus from me now!!
  6. #19
  7. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    Very interesting thread guys. Nice overview of OOP too. Thanks

    --Simon
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo