NameError when defining a class variable
up vote
1
down vote
favorite
Obviously I'm doing something stupid. But what?
I get:
File "<path>", line 71, in args
filename = filename
NameError: name 'filename' is not defined
...on the next-to-last line below ("filename = filename"):
def parseLog(filename, explain=False, omitminor=False, omitexpected=False,
omitgdocs=False, args=None):
print(filename)
if not args:
class args:
filename = filename
explain = explain
Yet the 2nd line above ("print(filename)") works fine. So, why the error?
In case you're wondering why I'm doing this in the first place, it's because the function parseLog() can also be called by the command line, like so:
def parseLogCLI():
''' parse command line for arguments '''
parser = argparse.ArgumentParser()
parser.add_argument('filename')
parser.add_argument('-explain', action="store_true", help='Explain what program has done')
parser.add_argument('-omitminor', action="store_true", help='Omit minor errors ' + repr(minor_errors))
parser.add_argument('-omitexpected', action="store_true", help='Omit machines expected to be often offline')
parser.add_argument('-omitgdocs', action="store_true", help='Omit errors on Google Docs native files (not copyable) ' + repr(gdocs))
args = parser.parse_args()
parseLog(arg.filename, args=args)
...so I'm trying to construct an 'arg' class (as argparse does) to pass to my function. If there's a better way to do this, I'm interested.
python
add a comment |
up vote
1
down vote
favorite
Obviously I'm doing something stupid. But what?
I get:
File "<path>", line 71, in args
filename = filename
NameError: name 'filename' is not defined
...on the next-to-last line below ("filename = filename"):
def parseLog(filename, explain=False, omitminor=False, omitexpected=False,
omitgdocs=False, args=None):
print(filename)
if not args:
class args:
filename = filename
explain = explain
Yet the 2nd line above ("print(filename)") works fine. So, why the error?
In case you're wondering why I'm doing this in the first place, it's because the function parseLog() can also be called by the command line, like so:
def parseLogCLI():
''' parse command line for arguments '''
parser = argparse.ArgumentParser()
parser.add_argument('filename')
parser.add_argument('-explain', action="store_true", help='Explain what program has done')
parser.add_argument('-omitminor', action="store_true", help='Omit minor errors ' + repr(minor_errors))
parser.add_argument('-omitexpected', action="store_true", help='Omit machines expected to be often offline')
parser.add_argument('-omitgdocs', action="store_true", help='Omit errors on Google Docs native files (not copyable) ' + repr(gdocs))
args = parser.parse_args()
parseLog(arg.filename, args=args)
...so I'm trying to construct an 'arg' class (as argparse does) to pass to my function. If there's a better way to do this, I'm interested.
python
I discovered that this works, but I don't know why: class args: pass; args.filename = filename
– nerdfever.com
Nov 10 at 20:03
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Obviously I'm doing something stupid. But what?
I get:
File "<path>", line 71, in args
filename = filename
NameError: name 'filename' is not defined
...on the next-to-last line below ("filename = filename"):
def parseLog(filename, explain=False, omitminor=False, omitexpected=False,
omitgdocs=False, args=None):
print(filename)
if not args:
class args:
filename = filename
explain = explain
Yet the 2nd line above ("print(filename)") works fine. So, why the error?
In case you're wondering why I'm doing this in the first place, it's because the function parseLog() can also be called by the command line, like so:
def parseLogCLI():
''' parse command line for arguments '''
parser = argparse.ArgumentParser()
parser.add_argument('filename')
parser.add_argument('-explain', action="store_true", help='Explain what program has done')
parser.add_argument('-omitminor', action="store_true", help='Omit minor errors ' + repr(minor_errors))
parser.add_argument('-omitexpected', action="store_true", help='Omit machines expected to be often offline')
parser.add_argument('-omitgdocs', action="store_true", help='Omit errors on Google Docs native files (not copyable) ' + repr(gdocs))
args = parser.parse_args()
parseLog(arg.filename, args=args)
...so I'm trying to construct an 'arg' class (as argparse does) to pass to my function. If there's a better way to do this, I'm interested.
python
Obviously I'm doing something stupid. But what?
I get:
File "<path>", line 71, in args
filename = filename
NameError: name 'filename' is not defined
...on the next-to-last line below ("filename = filename"):
def parseLog(filename, explain=False, omitminor=False, omitexpected=False,
omitgdocs=False, args=None):
print(filename)
if not args:
class args:
filename = filename
explain = explain
Yet the 2nd line above ("print(filename)") works fine. So, why the error?
In case you're wondering why I'm doing this in the first place, it's because the function parseLog() can also be called by the command line, like so:
def parseLogCLI():
''' parse command line for arguments '''
parser = argparse.ArgumentParser()
parser.add_argument('filename')
parser.add_argument('-explain', action="store_true", help='Explain what program has done')
parser.add_argument('-omitminor', action="store_true", help='Omit minor errors ' + repr(minor_errors))
parser.add_argument('-omitexpected', action="store_true", help='Omit machines expected to be often offline')
parser.add_argument('-omitgdocs', action="store_true", help='Omit errors on Google Docs native files (not copyable) ' + repr(gdocs))
args = parser.parse_args()
parseLog(arg.filename, args=args)
...so I'm trying to construct an 'arg' class (as argparse does) to pass to my function. If there's a better way to do this, I'm interested.
python
python
asked Nov 10 at 20:00
nerdfever.com
673619
673619
I discovered that this works, but I don't know why: class args: pass; args.filename = filename
– nerdfever.com
Nov 10 at 20:03
add a comment |
I discovered that this works, but I don't know why: class args: pass; args.filename = filename
– nerdfever.com
Nov 10 at 20:03
I discovered that this works, but I don't know why: class args: pass; args.filename = filename
– nerdfever.com
Nov 10 at 20:03
I discovered that this works, but I don't know why: class args: pass; args.filename = filename
– nerdfever.com
Nov 10 at 20:03
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
With class args:
you are starting the definition of a class.
In that context, the first occurrence of filename
defines a class attribute, which you are trying to assign from its own value before it is fully defined.
First of all, I think you should investigate in more details the concepts of classes, scopes, and instances.
This will help you understand why your function argument filename
is hidden by the new definition inside the class scope.
They can use a different name either for the function argument or the class attribute to fix this. However, I would rather find a way to refactor the code so you don't need to create classes at runtime. It's possible to do that in python, and there are valid use cases. But 99% of the time, this only adds complexity without significant benefit. stackoverflow.com/questions/100003/…
– Håken Lid
Nov 10 at 20:37
add a comment |
up vote
0
down vote
It seems to me like you have a scope issue, in that the inner class 'arg' doesn't have access to the scope of the outer class. One solution would be to use the 'global' keyword like so:
def parseLog(filename, explain=False, omitminor=False, omitexpected=False,
omitgdocs=False, args=None):
global fname, expl
fname = filename
expl = explain
print(filename)
if not args:
class args:
filename = fname
explain = expl
You can read more about Python variable scopes here.
1
global
doesn't fix this, but reassigning to a different name works.
– Håken Lid
Nov 10 at 20:44
@HåkenLidglobal
works if you specify a new variable name, but you're right in saying that you don't need to use it if you reassign the values to a different variable.
– Sahil Makhijani
Nov 10 at 21:01
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
With class args:
you are starting the definition of a class.
In that context, the first occurrence of filename
defines a class attribute, which you are trying to assign from its own value before it is fully defined.
First of all, I think you should investigate in more details the concepts of classes, scopes, and instances.
This will help you understand why your function argument filename
is hidden by the new definition inside the class scope.
They can use a different name either for the function argument or the class attribute to fix this. However, I would rather find a way to refactor the code so you don't need to create classes at runtime. It's possible to do that in python, and there are valid use cases. But 99% of the time, this only adds complexity without significant benefit. stackoverflow.com/questions/100003/…
– Håken Lid
Nov 10 at 20:37
add a comment |
up vote
2
down vote
accepted
With class args:
you are starting the definition of a class.
In that context, the first occurrence of filename
defines a class attribute, which you are trying to assign from its own value before it is fully defined.
First of all, I think you should investigate in more details the concepts of classes, scopes, and instances.
This will help you understand why your function argument filename
is hidden by the new definition inside the class scope.
They can use a different name either for the function argument or the class attribute to fix this. However, I would rather find a way to refactor the code so you don't need to create classes at runtime. It's possible to do that in python, and there are valid use cases. But 99% of the time, this only adds complexity without significant benefit. stackoverflow.com/questions/100003/…
– Håken Lid
Nov 10 at 20:37
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
With class args:
you are starting the definition of a class.
In that context, the first occurrence of filename
defines a class attribute, which you are trying to assign from its own value before it is fully defined.
First of all, I think you should investigate in more details the concepts of classes, scopes, and instances.
This will help you understand why your function argument filename
is hidden by the new definition inside the class scope.
With class args:
you are starting the definition of a class.
In that context, the first occurrence of filename
defines a class attribute, which you are trying to assign from its own value before it is fully defined.
First of all, I think you should investigate in more details the concepts of classes, scopes, and instances.
This will help you understand why your function argument filename
is hidden by the new definition inside the class scope.
answered Nov 10 at 20:10
Silmathoron
7391619
7391619
They can use a different name either for the function argument or the class attribute to fix this. However, I would rather find a way to refactor the code so you don't need to create classes at runtime. It's possible to do that in python, and there are valid use cases. But 99% of the time, this only adds complexity without significant benefit. stackoverflow.com/questions/100003/…
– Håken Lid
Nov 10 at 20:37
add a comment |
They can use a different name either for the function argument or the class attribute to fix this. However, I would rather find a way to refactor the code so you don't need to create classes at runtime. It's possible to do that in python, and there are valid use cases. But 99% of the time, this only adds complexity without significant benefit. stackoverflow.com/questions/100003/…
– Håken Lid
Nov 10 at 20:37
They can use a different name either for the function argument or the class attribute to fix this. However, I would rather find a way to refactor the code so you don't need to create classes at runtime. It's possible to do that in python, and there are valid use cases. But 99% of the time, this only adds complexity without significant benefit. stackoverflow.com/questions/100003/…
– Håken Lid
Nov 10 at 20:37
They can use a different name either for the function argument or the class attribute to fix this. However, I would rather find a way to refactor the code so you don't need to create classes at runtime. It's possible to do that in python, and there are valid use cases. But 99% of the time, this only adds complexity without significant benefit. stackoverflow.com/questions/100003/…
– Håken Lid
Nov 10 at 20:37
add a comment |
up vote
0
down vote
It seems to me like you have a scope issue, in that the inner class 'arg' doesn't have access to the scope of the outer class. One solution would be to use the 'global' keyword like so:
def parseLog(filename, explain=False, omitminor=False, omitexpected=False,
omitgdocs=False, args=None):
global fname, expl
fname = filename
expl = explain
print(filename)
if not args:
class args:
filename = fname
explain = expl
You can read more about Python variable scopes here.
1
global
doesn't fix this, but reassigning to a different name works.
– Håken Lid
Nov 10 at 20:44
@HåkenLidglobal
works if you specify a new variable name, but you're right in saying that you don't need to use it if you reassign the values to a different variable.
– Sahil Makhijani
Nov 10 at 21:01
add a comment |
up vote
0
down vote
It seems to me like you have a scope issue, in that the inner class 'arg' doesn't have access to the scope of the outer class. One solution would be to use the 'global' keyword like so:
def parseLog(filename, explain=False, omitminor=False, omitexpected=False,
omitgdocs=False, args=None):
global fname, expl
fname = filename
expl = explain
print(filename)
if not args:
class args:
filename = fname
explain = expl
You can read more about Python variable scopes here.
1
global
doesn't fix this, but reassigning to a different name works.
– Håken Lid
Nov 10 at 20:44
@HåkenLidglobal
works if you specify a new variable name, but you're right in saying that you don't need to use it if you reassign the values to a different variable.
– Sahil Makhijani
Nov 10 at 21:01
add a comment |
up vote
0
down vote
up vote
0
down vote
It seems to me like you have a scope issue, in that the inner class 'arg' doesn't have access to the scope of the outer class. One solution would be to use the 'global' keyword like so:
def parseLog(filename, explain=False, omitminor=False, omitexpected=False,
omitgdocs=False, args=None):
global fname, expl
fname = filename
expl = explain
print(filename)
if not args:
class args:
filename = fname
explain = expl
You can read more about Python variable scopes here.
It seems to me like you have a scope issue, in that the inner class 'arg' doesn't have access to the scope of the outer class. One solution would be to use the 'global' keyword like so:
def parseLog(filename, explain=False, omitminor=False, omitexpected=False,
omitgdocs=False, args=None):
global fname, expl
fname = filename
expl = explain
print(filename)
if not args:
class args:
filename = fname
explain = expl
You can read more about Python variable scopes here.
answered Nov 10 at 20:23
Sahil Makhijani
538
538
1
global
doesn't fix this, but reassigning to a different name works.
– Håken Lid
Nov 10 at 20:44
@HåkenLidglobal
works if you specify a new variable name, but you're right in saying that you don't need to use it if you reassign the values to a different variable.
– Sahil Makhijani
Nov 10 at 21:01
add a comment |
1
global
doesn't fix this, but reassigning to a different name works.
– Håken Lid
Nov 10 at 20:44
@HåkenLidglobal
works if you specify a new variable name, but you're right in saying that you don't need to use it if you reassign the values to a different variable.
– Sahil Makhijani
Nov 10 at 21:01
1
1
global
doesn't fix this, but reassigning to a different name works.– Håken Lid
Nov 10 at 20:44
global
doesn't fix this, but reassigning to a different name works.– Håken Lid
Nov 10 at 20:44
@HåkenLid
global
works if you specify a new variable name, but you're right in saying that you don't need to use it if you reassign the values to a different variable.– Sahil Makhijani
Nov 10 at 21:01
@HåkenLid
global
works if you specify a new variable name, but you're right in saying that you don't need to use it if you reassign the values to a different variable.– Sahil Makhijani
Nov 10 at 21:01
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242897%2fnameerror-when-defining-a-class-variable%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I discovered that this works, but I don't know why: class args: pass; args.filename = filename
– nerdfever.com
Nov 10 at 20:03