Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

What?...

Name: Jeremy 2012-08-15 5:26

Hi. I have been learning the super noob language python for a while now. I am trying to grasp classes and stuff but i am too retarded. Anybody help?

Name: Anonymous 2012-08-15 6:25

>>1
Hello, Jeremy. The solution to your problem is to stop being too retarded. You're welcome.

Name: Jeremy 2012-08-15 8:54

Oh gee buddy! It worked!

Name: Anonymous 2012-08-15 9:19

>>3
You are welcome, glad we could help!

Name: yus 2012-08-15 13:11

Is this your first language, OP?

Name: Anonymous 2012-08-15 13:58

Imagine a bunch of variables and functions glued together into one functional unit, and which you can copy into multiple instances and modify them individually as much as you like. That's a class.

class Dick(object):
    """ Docstring? MORE LIKE COCKSTRING AHAHAHA """
    def __init__(self):
        """ Define my cock """
        self.cocks = "hello world!"
    def penis(self):
        """ Print my cock """
        print self.cocks

a = Dick()
a.penis()
# redefine the penis
a.cocks = "nigger"
a.penis()

b = Dick()
# note that a and b have a different cock.
print "a's cock:", a.cocks
print "b's cock:", b.cocks


Also classes can be based on other classes:

class Anus(Dick):
    def __init__(self):
        """ This replaces the __init__ method of Dick """
        self.cocks = "faggot"

c = Anus()
c.penis()


By the way, those strings inside the class will show when you use help(Anus). The first, unbound string in a class, file or function automatically becomes the docstring that describes it.

Help on module prog:

NAME
    prog

FILE
    path/prog.py

CLASSES
    __builtin__.object
        Dick
            Anus
   
    class Anus(Dick)
     |  Method resolution order:
     |      Anus
     |      Dick
     |      __builtin__.object
     | 
     |  Methods defined here:
     | 
     |  __init__(self)
     |      This replaces the __init__ method of Dick
     | 
     |  ----------------------------------------------------------------------
     |  Methods inherited from Dick:
     | 
     |  penis(self)
     |      Print my cock
     | 
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from Dick:
     | 
     |  __dict__
     |      dictionary for instance variables (if defined)
     | 
     |  __weakref__
     |      list of weak references to the object (if defined)
   
    class Dick(__builtin__.object)
     |  Docstring? MORE LIKE COCKSTRING AHAHAHA
     | 
     |  Methods defined here:
     | 
     |  __init__(self)
     |      Define my cock
     | 
     |  penis(self)
     |      Print my cock
     | 
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     | 
     |  __dict__
     |      dictionary for instance variables (if defined)
     | 
     |  __weakref__
     |      list of weak references to the object (if defined)

DATA
    a = <test.Dick object>
    b = <test.Dick object>
    c = <test.Anus object>

Name: Anonymous 2012-08-15 14:02

DATA
        a = <prog.Dick object>
        b = <prog.Dick object>
        c = <prog.Anus object>

Name: Anonymous 2012-08-15 18:31

Is this your first thread on /prog/?

Name: Jeremy 2012-08-15 21:11

yes it is my first language. I am usually on the imageboards.

Name: Anonymous 2012-08-15 21:56

The only way to learn OOP is to actually do it.  Don't waste time reading about it.  Copy someone's Dog class and make it bark, walk, sit, name it, give it 5 legs, etc.  Extend the class, change up the class structure so that Dog inherits from Pet, etc.  Learn what concepts those big words were trying to convey. 

As long as you're playing with objects, you're learning how to handle them.  Slowly but surely you'll understand WHY you use a class and you'll KNOW (how/when) to use them.   But you have to write them, so go make the Dog bark.

Name: Jeremy 2012-08-15 22:04

Okay, Ill make the dog shit too :D.

Name: Anonymous 2012-08-15 22:05

The only way to learn MY ANUS is to actually do MY ANUS.  Don't waste time reading about MY ANUS.  Copy MY ANUS class and make it MY ANUS, MY ANUS, MY ANUS, name it, give it 5 MY ANUSs, etc.  Extend MY ANUS, change up MY ANUS structure so MY ANUS inherits from MY ANUS, etc.  Learn what concepts those big MY ANUS were trying to convey.

As long as you're playing with MY ANUS, you're learning how to handle MY ANUS.  Slowly but surely you'll understand WHY you use MY ANUS and you'll KNOW (how/when) to use MY ANUS.   But you have to write MY ANUS, so go make MY ANUS bark.

Name: Anonymous 2012-08-15 22:14

So you come from the imageboards. I'll help you anyway, but please fuck off back wherever you came from, please.

A class is like a template. Let's say I'm an inventor and I'm trying to invent inventions. I decided to make some stupid contraction that throws projectiles. I'll call it a catapult.

What the hell is this ``catapool" shit anyway? Well, what can I say. It has uh, wheels, and a silly rod. And maybe a maximum projectile weight.

But not all catapools have the same number of wheels, and not all catapults have rods of the same length. Hell, they might be even made of another material. They might even lack wheels. These are going to be the properties of my new invention.

So my template looks like this:

Template for a catapult:
    Wheels [ ]
    Rod length [ ]
    Material [ ]
    Maximum projectile weight [ ]

See how those [ ]s were left empty? It's because this is just a template. When you build an actual catapult, you're going to use this template as a base, and the actual catapult will fill these blanks according on the... well, actual catapult.

Roman catapult #1, made from Template for a catapult:
    Wheels [4]
    Rod length [2m]
    Material [Wood]
    Maximum projectile weight [5Kg]

Roman catapult #2, made from Template for a catapult:
    Wheels [4]
    Rod length [2m]
    Material [Wood]
    Maximum projectile weight [5Kg]

Roman elite catapult #10, made from Template for a catapult:
    Wheels [10]
    Rod length [4m]
    Material [Steel]
    Maximum projectile weight [50Kg]

See how the actual catapults share the template? And you can put whatever you want in the []s.

Name: Anonymous 2012-08-15 22:16

Mutating state considered harmful.

Name: Anonymous 2012-08-15 22:21

But catapults are not just wheels and rods. So we give our Template for a catapult some behaviors.

Template for a catapult:
    Wheels [ ]
    Rod length [ ]
    Material [ ]
    Maximum projectile weight [ ]

    (Action) Load:
        Lower rod to ground level
        Tell operator to put projectile on rod

    (Action) Fire:
        Is the projectile on the rod?
            Yes: Cut string
            No: Tell operator to load

    (Action) Move:
        Go forward

Then you could do:

List of orders:
----
Template for a catapult:
    Wheels [ ]
    Rod length [ ]
    Material [ ]
    Maximum projectile weight [ ]

AMERICAN modern catapult #4, made from Template for a catapult:
    Wheels [10]
    Rod length [4m]
    Material [Steel]
    Maximum projectile weight [100Kg]

Move(AMERICAN modern catapult #4)
----

This way you would create the template, make a catapult based on the template and use the built catapult.

Name: Anonymous 2012-08-15 22:30

Translating this to Python, uh, I don't know how, because Python OOP is fucking shit.

But if I recall correctly, it's something like this:


class Catapult:
    def ___init___(self, wheels, rod_length, material, max_weight):
        self.wheels = wheels
        self.rod_length = rod_length
        self.material = material
        self.max_weight = max_weight
        self.loaded = False
    
    def move(self):
        print "/###/"
        print "/O==/"
        print "/###/"
        print "@@@@@"
        print "pretend ^ is moving to the right"

    def load(self):
        print "/###/"
        print "/%==/"
        print "/###/"
        print "@@@@@"
        print "loaded"

    def fire():
        if self.loaded == True:
            print "@*~~*@ boom"
        else:
            print "load it"
################################

AMERICANcatapult = Catapult(10, 4, 'Steel', 100)

AMERICANcatapult.move()
AMERICANcatapult.fire()
AMERICANcatapult.load()
AMERICANcatapult.fire()

Name: Anonymous 2012-08-15 22:36

To sum up:

* Templates are usually called classes
* Properties are usually called member variables
* Behaviors are usually called methods
* The actual object made from a template is, well, called an object
* Python is fucking shit
* Read some book about OOP concepts in Java, it's going to be a lot easier than this FIOC piece of shit.
* Java is fucking shit too, it's only useful for learning OOP concepts

And now, go back to your home board and never come back. I'll fucking kill you if you ever bring one of your fellow imageboard shitposting le reddit retards here.

Name: Anonymous 2012-08-15 22:38

>>12
How do I get MY ANUS to bark?

Name: Anonymous 2012-08-15 23:02

Don't use classes

Use named tuples

  3DPoint = collections.namedtuple('3DPoint', ['x', 'y', 'z'])
  p1 = 3DPoint(3, 2, 4)
  p2 = 3DPoint(10, 1, 10)

  dist = math.sqrt((p2.x - p1.x)**2 +
                   (p2.y - p1.y)**2 +
                   (p2.z - p1.z)**2)

Name: Anonymous 2012-08-15 23:30

Don't use shoes

Use dried pieces of dog shit

<***.**>_##^>    <%$^<<<^%$>$%>>

  <<^<#$^#&&>$%%&>> <$%^<$%^^^^^^^^^^^^^^##>

Name: Anonymous 2012-08-16 18:11

Don't use dried pieces of dog shit

Use Lisp

Name: Anonymous 2012-08-16 19:45

Don't use Lisp

No wait do use Lisp

Name: Anonymous 2012-08-16 23:31

while not not not not True:
 print("poop")

Don't change these.
Name: Email:
Entire Thread Thread List