Object Oriented Programming With Python (part 1) - Digging Deep
(Page 4 of 6 )
Now that you've got the concepts straight, let's take a look at the nitty-gritty of a class definition.
class veryBigSnake:
# method and property definitions
Every class definition begins with the keyword "class",
followed by a class name. You can give your class any name that strikes your fancy, so long as it doesn't collide with a reserved word. All class variables and functions are indented within this block, and are written as you would normally code them.
In order to create a new instance of a class, you need to simply create a new variable referencing the class.
>>> alpha = veryBigSnake()
>>>
In English, the above would mean "create a new object of
class veryBigSnake and assign it to the variable 'alpha'".
You can now access all the methods and properties of the class via this variable.
>>> # accessing a method
>>> alpha.set_snake_name("Peter Python")
>>>
>>> # accessing a property
>>> alpha.name
>>>
Again, in English,
>>> alpha.set_snake_name("Peter Python")
>>>
would mean
"execute the method set_snake_name() with
parameter 'Peter Python' of this specific instance of the class veryBigSnake".
Next: Self-Involved >>
More Python Articles
More By icarus, (c) Melonfire