Re: [Tutor] defined()
Dear Tim, Dear Alan, I can't find the defined() function in python, so I used 'variable name' in dir() Is it really missing, or I am just so simple ? It is really missing, just as it is for most programming languages. Which language(s) do you know that has such a feature? I should came from Marco Cantu's Delphi 2005 book, that I have read just recently. But I am unable to find it again. And why do you consider it so useful that you expect to find it in Python? I don't miss it. It was just a foggy engram, that I couldn't find in the help :) The main place where I could see such a thing being useful would be in dynamically loaded code but then the usual approach is to load a dictionary and an 'in' check suffices. I'm interested in what use you would make of such a thing? I just started to make a .leo file, where I wanted to place my scripts. http://webpages.charter.net/edreamleo/front.html I just tried to collect all of my scripts (sql, wmi, admin, snmp ...), and html references, admin knowledge, passwords for active devices... into one place, that can be shared with my colleagues with detailed description about the usage and the reasons to use of them. Leo seems to be a very good candidate for that. An sql script seems to be like this. - ScalaDB data = Query( Sql ) Show Data In the leo file the ScalaDB is simple replaced by the ScalaDB subtree. The script is created dinamically from the texts in the tree. So I just wanted to check in the Show Data part, if the data is defined previously or not. Yours sincerely, __ Janos Juhasz ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] defined()
Which language(s) do you know that has such a feature? I should came from Marco Cantu's Delphi 2005 book, that I have read just recently. But I am unable to find it again. I'd be very surprised if it came from Delphi for two reasons: a) I used Delphi a lot for several years and never came across it! :-) b) Delphi (or Object Pascal) is strictly statically typed and wouldn't even compile with any undefined values in the code. BUT... I just checked and it *is* in Delphi - a new feature in Delphi 6. BUT it's not a language feature rather it is a compiler directive like the C #ifdef. (And starts with uppercase D BTW). His example: // const debugControl = 2 {$IF Defined(DEBUG) and DebugControl 3} // do stuff here {$IFEND} //- There is also a Declared directive too. These are not primarily intended for determining whether a variable is defined or declared but to determine whether a particular language feature has been defined or constant declared in the current version of Delphi (post version 6 of course!) Now that's an entirely different question in terms of how we do that in Python! Which I'll lreave as an excercise for the readers ;-) -- Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] defined()
Hi All, I can't find the defined() function in python, so I used 'variable name' in dir() for check if the variable defined. name = 'Joe' if 'name' in dir(): ... print name ... Joe Is it really missing, or I am just so simple ? Yours sincerely, __ János Juhász ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] defined()
[János Juhász] | I can't find the defined() function in python, so I used | | 'variable name' in dir() | | for check if the variable defined. | | name = 'Joe' | if 'name' in dir(): | ... print name | ... I'm not entirely sure where you'd want to use this, but probably the most Pythonic way of doing this would be: code name = Joe try: name except NameError: print name not defined else: print name defined /code I suspect that your idea of variable definition doesn't quite match Python's concept. In short, it's impossible to declare a variable in Python without binding it to *something*. ie a variable is always a binding to an object, not a hole waiting to be filled. You could, if you wanted, initialise name to None (or some other sentinel value) and then check against that, either explicitly: if name is None: print name unitialised or by taking advantage of the fact that several empty objects in Python are considered False: if not Name: print name unitialised Hope that helps more than it confuses. TJG This e-mail has been scanned for all viruses by Star. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] defined()
I can't find the defined() function in python, so I used 'variable name' in dir() Is it really missing, or I am just so simple ? It is really missing, just as it is for most programming languages. Which language(s) do you know that has such a feature? And why do you consider it so useful that you expect to find it in Python? The main place where I could see such a thing being useful would be in dynamically loaded code but then the usual approach is to load a dictionary and an 'in' check suffices. I'm interested in what use you would make of such a thing? -- Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] defined()
Alan Gauld wrote: Which language(s) do you know that has such a feature? And why do you consider it so useful that you expect to find it in Python? I'm not the original poster, but being a perlhead before, I can say it exists in Perl. It is very often used too. I used to miss it at first, but normally I now do the right thing semantically. Counting on the variable being defined or not is simply another bit of information. Assigning a value for that case is much cleaner. It is not used for black magic in Perl, AFAIK. Just normal testing. Hugo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] defined()
Hugo González Monteverde wrote: Alan Gauld wrote: Which language(s) do you know that has such a feature? And why do you consider it so useful that you expect to find it in Python? I'm not the original poster, but being a perlhead before, I can say it exists in Perl. It is very often used too. And you can roll your own in Python: def defined(name): return name in globals() I used to miss it at first, but normally I now do the right thing semantically. Counting on the variable being defined or not is simply another bit of information. Assigning a value for that case is much cleaner. It is not used for black magic in Perl, AFAIK. Just normal testing. Hugo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] defined()
* Alan Gauld [EMAIL PROTECTED] [060403 09:10]: I can't find the defined() function in python, so I used 'variable name' in dir() Is it really missing, or I am just so simple ? It is really missing, just as it is for most programming languages. Which language(s) do you know that has such a feature? And why do you consider it so useful that you expect to find it in Python? In rebol, there is a predicate called value? Sample console session below: test: [a 1 b 2 c 3] == [one 1 two 2 three 3] value? test/1 == false value? test/2 == true == [a 1 b 2 c 3] test/a == 1 Don't as much about lisp as I do rebol and python, but lisp has symbols, which don't necessarily have values. The main place where I could see such a thing being useful would be in dynamically loaded code but then the usual approach is to load a dictionary and an 'in' check suffices. Rebol doesn't have dictionaries (it should IMHO), you could also use value? after importing a module to check if some word existed in the module namespace. Kind of like hasattr() I'm interested in what use you would make of such a thing? My business partner is a perl programmer. He uses defined() a lot, I think, I've seen it in his code I use value? a lot in rebol. I like python's in operator. Very handy tim -- Tim Johnson [EMAIL PROTECTED] http://www.alaska-internet-solutions.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] defined()
I'm interested in what use you would make of such a thing? My business partner is a perl programmer. He uses defined() a lot, I think, I've seen it in his code Hello! The common idiom in Perl, I think, is to at least declare the variable, even if one doesn't give an initial value, like this: # ## Perl pseudocode ## use strict; my $name; ## do things here that should initialize name if (! defined($name)) { # handle degenerate case here } # But this is very different than: # ## Perl pseudocode that doesn't use strict ## do things here that should initialize name if (! defined($name)) { # handle degenerate case here } # Now, if your business partner doesn't have the line 'use strict' in their code, then give them a good kick and tell them to use it! It's criminal for a professonal Perl programmer not to use strict, and I feel almost foolish about bringing this up. But it has to be said, just in case. *grin* In Python, the first assignment to a variable name has the same effect as declaration, so the first Perl snippet has a translation like: # ## Python pseudocode name = None ## do things here that should initialize name if name is None: ## handle degenerate case here # where we can use None as our uninitialized value. Hope this helps! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] defined()
* Danny Yoo [EMAIL PROTECTED] [060403 18:14]: I'm interested in what use you would make of such a thing? My business partner is a perl programmer. He uses defined() a lot, I think, I've seen it in his code Now, if your business partner doesn't have the line 'use strict' in their code, then give them a good kick and tell them to use it! It's criminal for a professonal Perl programmer not to use strict, I'm sure he has his stay out of jail card. I know he always uses it. -- Tim Johnson [EMAIL PROTECTED] http://www.alaska-internet-solutions.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor