Newbie Tip: Classes and variable passing vs instances from ENGSOL

engsol at teleport.com engsol at teleport.com
Thu Aug 2 00:12:28 EDT 2001


I'm brand new to Python, and am reading all the books I can, as I'm sure the
other "newbies" are. As a result, I breadboard "stuff" to see how they work.

Seems a waste to to just let the scripts idle on my hard drive, so from time to
time I'll post them, with the hope they will be usefu to someonel. Bear in mind
I am using NT4, and Python21. No warrenty of course. <grin>

Here's the first, regarding classes/variables/instances:

class CLASS_4:

  def __init__(self_1, input):
      print "from the \"__init__ thing\"  :   " + input

  def get_foo_bar(self_2, value):
      self_2.foo_bar = value
      print "print from \"get_foo_bar\"   :   " + value

  def print_foo_bar(self_3):
      print "print from \"print_foo_bar\" :   " + self_3.foo_bar

""" Notice:
    While 'self' is the usual *name*, we can call it anything we like.
    Here I've used the name "self_1, self_2, self_3", to make the point.
    It's obvious that they are mutually exclusive in this case.
"""
# End CLASS_4

# Now run the program:

instance_1 = CLASS_4("input from instance_1 the first time")
instance_2 = CLASS_4("input from instance_2 the first time")

instance_1.get_foo_bar("hello from instance_1 after calling \"get_foo_bar\"")
instance_2.get_foo_bar("world from instance_2 after calling \"get_foo_bar\"")

instance_1.print_foo_bar()
instance_2.print_foo_bar()

# End module class_4.py

""" PROGRAM RESULTS:

from the "__init__ thing"  :   input from instance_1 the first time
from the "__init__ thing"  :   input from instance_2 the first time
print from "get_foo_bar"   :   hello from instance_1 after calling "get_foo_bar"
print from "get_foo_bar"   :   world from instance_2 after calling "get_foo_bar"
print from "print_foo_bar" :   hello from instance_1 after calling "get_foo_bar"
print from "print_foo_bar" :   world from instance_2 after calling "get_foo_bar"

"""









More information about the Python-list mailing list