943,735 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1458
  • Python RSS
Jan 19th, 2007
0

Using a Variable to Identify an Instance

Expand Post »
I'd like to set up something like this:
Python Syntax (Toggle Plain Text)
  1. a=0
  2. class b:
  3. def__init__(self, foo):
  4. self.foo = foo
  5. def create():
  6. global a
  7. a=a+1
  8. a=b("FOO")
Where the number 1 will define the instance (I'm not sure if define is the write word, whatever you put before the equals sign when your creating an instance)

The problem is, I can't. Obviously a would identify the instance instead of 1. I tried using dictionaries, but again the computer took it leterally - d{x} was the identity, not 1. Any suggestions?
[/code]
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
danielfolsom is offline Offline
1 posts
since Jan 2007
Jan 19th, 2007
0

Re: Using a Variable to Identify an Instance

I'd like to set up something like this:
Python Syntax (Toggle Plain Text)
  1. a=0
  2. class b:
  3. def__init__(self, foo):
  4. self.foo = foo
  5. def create():
  6. global a
  7. a=a+1
  8. a=b("FOO")
Where the number 1 will define the instance (I'm not sure if define is the write word, whatever you put before the equals sign when your creating an instance)

The problem is, I can't. Obviously a would identify the instance instead of 1. I tried using dictionaries, but again the computer took it leterally - d{x} was the identity, not 1. Any suggestions?
[/code]
Well, I'm a little confused. In this case, do you want to create an instance of class b with foo == 1?

If so, then you would do this:

Python Syntax (Toggle Plain Text)
  1. class b:
  2. def __init__(self, foo)
  3. self.foo = foo
  4.  
  5. a = b(foo=1) # Create instance of b class, assign it to a.

or even shorter,

Python Syntax (Toggle Plain Text)
  1. a = b(1) # Python will automagically assign self = b and foo = 1.
Some other thoughts:

(1) It's best to make all your classes the 'new style' class

class b(object): # this inherits from Python's 'object' base class

rather than

class b:

because it will allow things like public properties. You need something like Python 2.3 or higher to use new-style classes.

(2) By convention, class names are capitalized.

class B(object):

(3) Global variables are like garlic: best used sparingly.

Hope it helps,
Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: a simple notepad in wxPython
Next Thread in Python Forum Timeline: Jpg in tkinter





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC