Grasshopper

algorithmic modeling for Rhino

hi guys, beginner Phyton Question here, pardon for this obvious question, I am not familiar with computer language at all so this is totally a new stuff for me, hope you guys can be patient enough as I will be asking alot for the next few months.

I am currently learning python on youtube, and then go through this "class" thing. basically the tutor said to type these lines:

class myClass:
    
    def __init__(self, _strCurve01, _strCurve02):
        self.strCurve01 = _strCurve01
        self.strCurve92 = _strCurve02
    
    def drawLineBetweenCurves(self):
        print ("everything is ok so far")

I got a little bit confused what is the logic behind those "__init__" and why bother typing "self" on that line? can someone explain to me the logic behind that? wouldn't the command be still okay if I write without "__Init__" and "self" stuff thrown?

for instance:

class myClass:

    def (_strCurve01, _strCurve02)

I am 100% sure I am wrong so can anyone explain a little bit more without throwing too much "computer jargon" considering I am still python newbie.

thanks :)

Views: 834

Replies to This Discussion

Hey Runnie,

Classes can take a bit of time to get your head round. For the largest part I'm self taught in most of my coding know how. I say this as my terminology may be slightly different to someone who has been using Python or any other Object Oriented language more consistently. In your example:

class myClass:

    
    def __init__(self, _strCurve01, _strCurve02):
        self.strCurve01 = _strCurve01
        self.strCurve92 = _strCurve02
    
    def drawLineBetweenCurves(self):
        print ("everything is ok so far")

"""Assign new object of blueprint type 'myClass' to variable x"""

x = myClass(crv1, crv2)

'__init__' is a function that is automatically called when you try to create an instance of your Class. It's often referred to as a constructor. This basically says to python you want to create an object (assigned to x in the above example) based on your blueprint called 'myClass'. For Python to do this it needs a '_strCurve01' and a '_strCurve02'.

'self' (in my understanding) gives the functions and variables of your class a context. It tells python that the variables you're give the '__init__' function belong to that the instance you've created. In this case I assigned an instance of 'myClass' to x.

I'm sure you've done some googleing but see this thread on stakeoverflow:

http://stackoverflow.com/questions/625083/python-init-and-self-what...

Hopefully that makes some sense? The only way I got my head around classes was trial and error. Good luck.

a little bit hard to digest but thank you for giving me some insight :) there are couple of points tho, I don't think I understand "class" good enough so I might as well ask you about that,

do classes always have "def" as it's property? if that so, what make "classes" different from "def"?

what is the main use of class? (just to be specific, in terms of generating 3D objects?)

thank you mate

Runnie,

Classes allow you to build custom defined objects. For example: Rhino points are a custom class which is made up of an X, Y and Z coord. The simplyfied version might look like this:

class Point3D:

    """Class blue print for a 3D point."""

    def __init__(self, x, y, z):

        #These lines link the inputs you give the class with the point you're creating

        self.x = x    

        self.y = y

        self.z = z

pt = Point3D(x=1.0, y=1.0, z=1.0) #creates new Point3D object and assigns it to 'pt'

'def' is used to define a function. For example we could include a function in our point object that measures the distance from that point to another point somewhere else. Or for a 'curve' object it might return the length.

For an interactive course on more general python coding (including classes and how to use them) have a look here: http://www.codecademy.com/learn

RSS

About

Translate

Search

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service