chemicalfan's Profile User Rating: -----

Reputation: 4 Apprentice
Group:
Active Members
Active Posts:
80 (0.06 per day)
Joined:
16-October 09
Profile Views:
2,472
Last Active:
User is offline Apr 16 2012 06:02 AM
Currently:
Offline

Previous Fields

Country:
GB
OS Preference:
Linux
Favorite Browser:
Chrome
Favorite Processor:
Intel
Favorite Gaming Platform:
PC
Your Car:
Volkswagen
Dream Kudos:
0

Latest Visitors

  • PhotoHiram Icon
    26 Aug 2011 - 06:51
Icon   chemicalfan has not set their status

Posts I've Made

  1. In Topic: creating a subclass changing only 50% of items from list of items

    Posted 9 Sep 2011

    So 50% of the snowflakes are of the subclass? Just apply the random statement with an if statement when you instantiate it. Something like:

    import random
    random.seed()
    
    x = randint(0,1)
    if x == 1:
        SF = Snowflake()
    else:
        SF = Windy()
    


    I'm not sure how you're naming the instances (I've picked SF), but it should mean 50% of objects are Snowflakes, and 50% are Windy
  2. In Topic: Nested objects - need clarity!

    Posted 8 Sep 2011

    Thanks, that's closer to how I'd do it (though in reality, I'd probably use a mix of both examples). This is why I can't understand "The Zen of Python" - there are many ways to do things in Python, it depends how you work.

    Anyway, I think I'm clear in my head now - as long as I keep abstraction/encapsulation only "1 deep", it'll be fine. So, A has a B, B has a C, but A doesn't reference C directly at all, only through B's methods (through it's own methods!)

    I need to write smaller methods I think, but more of them. Methods that do one or two things themselves, then call another method. Ugh, one day I'll get this thing off the ground - don't even have the metaphorical wings built, let alone attached to the rest of the metaphorical plane, lol
  3. In Topic: Nested objects - need clarity!

    Posted 8 Sep 2011

    View PostMotoma, on 07 September 2011 - 09:05 AM, said:

    class bullet:
        def fire(self):
            print("Bang!")
    
    class magazine:
        def __init__(self):
            self.max = 10
            self.bullets = []
        def fill(self.):
            while len(self.bullets) < self.max:
                self.bullets.append(bullet())
        def fire(self):
            if len(self.bullets) == 0:
                print("Click")
            else:
                bullet = self.bullets.pop()
                bullet.fire()
    
    class gun:
        def __init__(self):
            self.magazine = magazine()
        def load(self):
            [b]mag[/b] = magazine()
            [b]mag[/b].fill()
            self.magazine = mag
        def fire(self):
            self.magazine.fire()
    
    class person:
        def __init__(self):
            self.gun = gun()
        def fire(self, times = 1):
            for i in range(times):
                self.gun.fire()
        def reload(self):
            self.gun.load()
    
    if __name__ == "__main__":
        billy = person()
        billy.reload()
        billy.fire(3)
        billy.reload()
        billy.fire(11)
    
    


    Quick question - why aren't the bolded parts a "self.xxxxx"? And what's the "self.magazine = mag" about? I mean, why not just "self.magazine = magazine()" for the first line?

    Thanks very much for that guide, it's awesome, really helped a lot :D

    Edit: Haha, they didn't bold because of the code tags - it's the load method in the gun class I'm talking about
  4. In Topic: Nested objects - need clarity!

    Posted 7 Sep 2011

    Thanks motoma, that's a great model of what I'm trying to do :)
    Got lots of encapsulation in the project, I'm hoping to hook a GUI up to the back end eventually, and I want the calls from it to be as simple as possible.

    Now to digest that example a few dozen times, get it clear in my head ;)

    Baavgai - Thanks, I know about the lack of private variables in Python, I'm not too worried as exploits aren't something that bother me with this project. It's the lack of methods that's stung me - I thought I had lots already, but now I think I've got a lot more to do!
  5. In Topic: Nested objects - need clarity!

    Posted 7 Sep 2011

    Hmm, I kinda wanted that "attrib1" part to be hidden from __main__. The A object shouldn't be interacted with directly. In my actual program, it won't be printed, but the value will need to be stored and used by other methods (and accessed by the "parent" objects, for want of a better phrase!). Should I have attempted to return something from "morestuff"? Ugh, still confused :(

    Maybe I'd be better off defining this diagramatically:

    --A
      |--B
      |  |--C
      |
      |--D
      |--E
    


    Remember, this isn't inheritance - there's no need for C to access B's methods or attributes. C is an attribute of B, just like other strings & integers. It's just rather than string methods, C has it's own defined methods. It just gets messy when trying to call them from B's methods (you end up with self.C.foo() which is fine if it was just 1 or 2, but I've got loads). Maybe I'm thinking about this all wrong, maybe I should be looking at this in a different way....?

    Edit: Doh, diagram got messed up, hold on....

    Edit 2: That's better :P

My Information

Member Title:
D.I.C Head
Age:
29 years old
Birthday:
August 31, 1983
Gender:
Location:
Portsmouth, UK
Programming Languages:
Python

Contact Information

E-mail:
Private
Website URL:
Website URL  http://

Friends

chemicalfan hasn't added any friends yet.

Comments

Page 1 of 1
  1. Photo

    Hiram Icon

    26 Aug 2011 - 06:51
    Always good to see a fellow Arch user! :3
Page 1 of 1