[issue34076] Nested loop in dictionary comprehension gives `global name not defined` inside class

2018-07-10 Thread Jan Christoph


Jan Christoph  added the comment:

Updated example with reversed variable order for reference. This really seems 
to be related to issue3692, but really not the same thing.

IMHO both `a` and `b` should be passed in a situation like this:


a = range(5)
b = range(3)
c = [x+y for x in a for y in b]


--
Added file: https://bugs.python.org/file47681/what_a_dict.py

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34076] Nested loop in dictionary comprehension gives `global name not defined` inside class

2018-07-10 Thread Jan Christoph


Jan Christoph  added the comment:

Okay, so we're a in another scope inside the dictionary comprehension (all 
comprehensions for that matter), and only one symbol is passed to the inside.

That's why `strange_reversed_working = {x+s.replace('(+/-)',''):None for x in 
infts.split(', ') for s in ('+','-','')}` functions, but if you reverse the 
order it does not. That's a real trap.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34076] Nested loop in dictionary comprehension gives `global name not defined` inside class

2018-07-10 Thread Jan Christoph


Jan Christoph  added the comment:

But the simpler dictionary compprehension `{s.replace('(+/-)',''):None for s in 
infts.split(', ')}` works perfectly. Shouldn't that also give the error if it 
was a scope issue?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34076] Nested loop in dictionary comprehension gives `global name not defined` inside class

2018-07-10 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

This looks like a duplicate of issue3692.

--
nosy: +serhiy.storchaka
resolution:  -> duplicate
stage:  -> resolved
status: open -> closed
superseder:  -> improper scope in list comprehension, when used in class 
declaration

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34076] Nested loop in dictionary comprehension gives `global name not defined` inside class

2018-07-09 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

I think this is explained in the below answers with example that the left most 
loop in the comprehension has access to the class variables which the nested 
comprehensions don't have

https://stackoverflow.com/a/22692274/2610955

https://stackoverflow.com/a/13913933/2610955

Thanks

--
nosy: +xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34076] Nested loop in dictionary comprehension gives `global name not defined` inside class

2018-07-09 Thread Jan Christoph


New submission from Jan Christoph :

The python code: 
```
class _tri(object):
infts = '(+/-)inf, (+/-)infty, (+/-)infinity'

strange_failing = {x+s.replace('(+/-)',''):None for x in ('+','-','') for s 
in infts.split(', ')}
```

gives a `global name 'infts' is not defined` exception, when normal dictionary 
comprehensions (without nested loops) and regular nested for-loops work 
perfectly well.

For a complete shell session and more illustrative example in versions 2.7.15 
and 3.6.4 see: https://pastebin.ubuntu.com/p/9Pg8DThbsd/

--
components: Interpreter Core
files: what_a_dict.py
messages: 321326
nosy: con-f-use
priority: normal
severity: normal
status: open
title: Nested loop in dictionary comprehension gives `global name not defined` 
inside class
type: behavior
versions: Python 2.7
Added file: https://bugs.python.org/file47678/what_a_dict.py

___
Python tracker 
<https://bugs.python.org/issue34076>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: global name not defined

2006-05-23 Thread bruno at modulix
NetKev wrote:
(snip)
 def process_log(self, logfile, offset):
 if new_denied_hosts:
 info(new denied hosts: %s, str(new_denied_hosts))
 [warn_Admin(ip) for ip in new_denied_hosts]

This uselessly builds a list. List comprehension is meant to create
lists, not to replace for loops.

  for ip in new_denied_hosts:
  warn_admin(ip)


-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: global name not defined

2006-05-23 Thread NetKev
good point

-- 
http://mail.python.org/mailman/listinfo/python-list


global name not defined

2006-05-22 Thread NetKev
I added a function 'warn_Admin' and defined it just before another
function 'process_log'.  'process_log' calls this warn_Admin' function.
 However, when it gets called i get the following error every time:
---
Traceback (most recent call last):
  File /usr/bin/denyhosts.py, line 202, in ?
first_time, noemail, daemon)
  File /usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py, line
86, in __init__
last_offset)
  File /usr/lib/python2.3/site-packages/DenyHosts/daemon.py, line 74,
in createDaemon
apply(func, args)
  File /usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py, line
137, in runDaemon
purge_time, purge_sleep_ratio)
  File /usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py, line
178, in daemonLoop
last_offset = self.process_log(logfile, last_offset)
  File /usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py, line
380, in process_log
[warn_Admin(ip) for ip in new_denied_hosts]
NameError: global name 'warn_Admin' is not defined
--
If I take the two functions out of their current environment and store
them in test file and run it, it doesn't complain.  I'm new  to python
so I'm guessing there is some weird scope rule I am missing.  I did try
'self.warn_Admin(ip)' just to be safe but then I got a 'too many
arguments' error?

I'm lost :)

the added function plus the header of the existing function(its too
large):

 def warn_Admin(warn_ip):
SENDMAIL = /usr/sbin/sendmail # sendmail location
p = os.popen(%s -t % SENDMAIL, w)
p.write(To: [EMAIL PROTECTED])
p.write(Subject: test from denyhosts\n)
p.write(\n) # blank line separating headers from body
p.write(Some text\n)
p.write(warn_ip)
sts = p.close()
if sts != 0:
info(Sendmail exit status: %s, sts)
return sts


def process_log(self, logfile, offset):
-

the call to warn_Admin from process_log:
---
if new_denied_hosts:
info(new denied hosts: %s, str(new_denied_hosts))
#[info(ip) for ip in new_denied_hosts]
[warn_Admin(ip) for ip in new_denied_hosts]
else:
debug(no new denied hosts)

-kevin

-- 
http://mail.python.org/mailman/listinfo/python-list


global name not defined

2006-05-22 Thread NetKev
I added a function 'warn_Admin' and defined it just before another
function 'process_log'.  'process_log' calls this warn_Admin' function.
 However, when it gets called i get the following error every time:
---
Traceback (most recent call last):
  File /usr/bin/denyhosts.py, line 202, in ?
first_time, noemail, daemon)
  File /usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py, line
86, in __init__
last_offset)
  File /usr/lib/python2.3/site-packages/DenyHosts/daemon.py, line 74,
in createDaemon
apply(func, args)
  File /usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py, line
137, in runDaemon
purge_time, purge_sleep_ratio)
  File /usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py, line
178, in daemonLoop
last_offset = self.process_log(logfile, last_offset)
  File /usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py, line
380, in process_log
[warn_Admin(ip) for ip in new_denied_hosts]
NameError: global name 'warn_Admin' is not defined
--
If I take the two functions out of their current environment and store
them in test file and run it, it doesn't complain.  I'm new  to python
so I'm guessing there is some weird scope rule I am missing.  I did try
'self.warn_Admin(ip)' just to be safe but then I got a 'too many
arguments' error?

I'm lost :)

-kevin

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: global name not defined

2006-05-22 Thread NetKev
google groups told me it had a server error the first time i posted
this.  Sorry for the dupe.  Anyways the second one is more complete.
Also, I made a mistake in my vocabulary.  These are methods and not
functions.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: global name not defined

2006-05-22 Thread Paul McGuire
NetKev [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 I added a function 'warn_Admin' and defined it just before another
 function 'process_log'.  'process_log' calls this warn_Admin' function.
  However, when it gets called i get the following error every time:
 ---
 Traceback (most recent call last):
   File /usr/bin/denyhosts.py, line 202, in ?
 first_time, noemail, daemon)
   File /usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py, line
 86, in __init__
 last_offset)
   File /usr/lib/python2.3/site-packages/DenyHosts/daemon.py, line 74,
 in createDaemon
 apply(func, args)
   File /usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py, line
 137, in runDaemon
 purge_time, purge_sleep_ratio)
   File /usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py, line
 178, in daemonLoop
 last_offset = self.process_log(logfile, last_offset)
   File /usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py, line
 380, in process_log
 [warn_Admin(ip) for ip in new_denied_hosts]
 NameError: global name 'warn_Admin' is not defined
 --
 If I take the two functions out of their current environment and store
 them in test file and run it, it doesn't complain.  I'm new  to python
 so I'm guessing there is some weird scope rule I am missing.  I did try
 'self.warn_Admin(ip)' just to be safe but then I got a 'too many
 arguments' error?

 I'm lost :)

 the added function plus the header of the existing function(its too
 large):
 
  def warn_Admin(warn_ip):
 SENDMAIL = /usr/sbin/sendmail # sendmail location
 p = os.popen(%s -t % SENDMAIL, w)
 p.write(To: [EMAIL PROTECTED])
 p.write(Subject: test from denyhosts\n)
 p.write(\n) # blank line separating headers from body
 p.write(Some text\n)
 p.write(warn_ip)
 sts = p.close()
 if sts != 0:
 info(Sendmail exit status: %s, sts)
 return sts


 def process_log(self, logfile, offset):
 -

 the call to warn_Admin from process_log:
 ---
 if new_denied_hosts:
 info(new denied hosts: %s, str(new_denied_hosts))
 #[info(ip) for ip in new_denied_hosts]
 [warn_Admin(ip) for ip in new_denied_hosts]
 else:
 debug(no new denied hosts)

 -kevin


Sounds like warn_Admin is defined within a class.
a. could not resolve name when call was not qualified with self.
b. when called as self.warn_Admin, name was resolved, but got too many
arguments - this is because there was no explicit self argument in the
definition of warn_Admin.

It doesn't look like warn_Admin needs to be in the class.  Move warn_Admin
to module-level scope, outside of the class containing process_log, and see
if things work better.

-- Paul


-- Paul


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: global name not defined

2006-05-22 Thread NetKev
You are probably right and I think I will do so but just for the sake
of my understanding of python...I noticed somthing.  process_log takes
two arguments when called but it's definition has 3 and one of them is
self.  So I'm thinking if I modify my warn_Admin definition to
include self and then call it from process_log with
self.warn_Admin... it will work.  This explains why I was getting the
too many arguments error.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: global name not defined

2006-05-22 Thread Paul McGuire
NetKev [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 You are probably right and I think I will do so but just for the sake
 of my understanding of python...I noticed somthing.  process_log takes
 two arguments when called but it's definition has 3 and one of them is
 self.  So I'm thinking if I modify my warn_Admin definition to
 include self and then call it from process_log with
 self.warn_Admin... it will work.  This explains why I was getting the
 too many arguments error.


Yes.  When you invoke self.warn_Admin(x), it calls warn_Admin with 2 args,
self and x.

The reason I did not suggest this is becaus it looked like warn_Admin didn't
really use anything inside self, so why make it a method?

Looks like you are getting the method/function concepts straight.

(I'm not trying to confuse you, but you could also make warn_Admin a
staticmethod within the class, using the @staticmethod decorator.  Static
methods do not pass the self argument, so making warn_Admin into a static
method would be another way to resolve this problem.  But only do this if
your class, whatever it is, has something inherently about it that wants its
own warn_Admin method - otherwise, just make it a global function.)

-- Paul


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: global name not defined :$

2005-06-20 Thread Fredrik Lundh
Anna M. [EMAIL PROTECTED] wrote:

 I am trying to write a red-black tree implementation in python.  I am very
 new to python and appologize if my question is terribly stubid.  But I ran
 into some trouble.  I have a class and in it there are functions but when I
 try to run the code I have I just get an error on one of the functions
 global name 'balancedFourNode' is not defined  Now of course I realize
 that I am doing something wrong when defining the function but I just can't
 seem to find it.

 Any insight would be greatly appreciated.

classes have methods, not functions.

if x is an instance of a class and method is a method defined by that
class, you should use x.method() to call the method.

if you're trying to call the method from another method in the same class,
use self.method().

if you type method or method(x), Python won't know where to look.

/F 



-- 
http://mail.python.org/mailman/listinfo/python-list