| | |
Using a Variable to Identify an Instance
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2007
Posts: 1
Reputation:
Solved Threads: 0
I'd like to set up something like this:
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]
Python Syntax (Toggle Plain Text)
a=0 class b: def__init__(self, foo): self.foo = foo def create(): global a a=a+1 a=b("FOO")
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]
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
•
•
•
•
I'd like to set up something like this:
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)Python Syntax (Toggle Plain Text)
a=0 class b: def__init__(self, foo): self.foo = foo def create(): global a a=a+1 a=b("FOO")
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]
If so, then you would do this:
Python Syntax (Toggle Plain Text)
class b: def __init__(self, foo) self.foo = foo a = b(foo=1) # Create instance of b class, assign it to a.
or even shorter,
Python Syntax (Toggle Plain Text)
a = b(1) # Python will automagically assign self = b and foo = 1.
(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
![]() |
Similar Threads
- Sending a dos environment variable to fstream instance (C++)
- help with creating and calling a function (C++)
- what is the diffarance between object and varible (Java)
- A few questions on classes!.. (C++)
- accessibility (Java)
- Variable Variable Names (C)
- How to access a Public Pointer value of a class directly? (C++)
- Help with casting (Java)
Other Threads in the Python Forum
- Previous Thread: a simple notepad in wxPython
- Next Thread: Jpg in tkinter
Views: 1296 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Python
address ansi backend beginner changecolor class client code conversion coordinates copy curves customdialog dan08 dictionary directory dynamic edit editing examples excel feet file float font format ftp function generator getvalue gui halp homework i/o images import info input ip java line linux list lists loop microcontroller mouse mysql newb number numbers output panel parsing path port prime program programming projects py2exe pygame pyqt python queue random rational recursion recursive schedule screensaverloopinactive scrolledtext searchingfile socket ssh statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython





