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.










share|improve this question





















  • I discovered that this works, but I don't know why: class args: pass; args.filename = filename
    – nerdfever.com
    Nov 10 at 20:03














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.










share|improve this question





















  • I discovered that this works, but I don't know why: class args: pass; args.filename = filename
    – nerdfever.com
    Nov 10 at 20:03












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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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












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.






share|improve this answer




















  • 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

















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.






share|improve this answer
















  • 1




    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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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.






share|improve this answer




















  • 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














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.






share|improve this answer




















  • 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












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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












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.






share|improve this answer
















  • 1




    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














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.






share|improve this answer
















  • 1




    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












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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å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












  • 1




    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







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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党