Objects
A very basic class would look something like this:
class MyClass:
variable = "blah"
def function(self):
print "This is a message inside the class."
We'll explain why you have to include that "self" as a parameter a little bit later. First, to assign the above class(template) to an object you would do the following:
myobjectx = MyClass()
Now the variable "myobjectx" holds an object of the class "MyClass" that contains the variable and the function defined within the class called "MyClass".
Accessing Object Variables
To access the variable inside of the newly created object "myobjectx" you would do the following:myobjectx.variable
So for instance the below would output the string "blah":
print myobjectx.variable
Accessing Object Functions
To access a function inside of an object you use notation similar to accessing a variable:myobjectx.function()
class MyClass:
variable = "blah"
def function(self):
print "This is a message inside the class."
myobjectx = MyClass()
print myobjectx.variable
myobjectx.function()
No comments:
Post a Comment