Thread: code reuse?

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

    Join Date
    Mar 2003
    Hull, UK
    Rep Power
    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,

    programming language development: Hula

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

    Join Date
    Feb 2005
    Rep Power
    Better would be to pass parameters in when Element() is created:
    class Element(object):
        def __init__(self, symbol, name, weight):
            self.symbol = symbol
   = 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)
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Rep Power
    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
    The Laboratory
    Rep Power
    Very interesting thread guys. Nice overview of OOP too. Thanks

Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo