Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #45786 > unrolled thread
| Started by | lokeshkoppaka@gmail.com |
|---|---|
| First post | 2013-05-23 02:51 -0700 |
| Last post | 2013-05-23 22:05 -0700 |
| Articles | 8 — 4 participants |
Back to article view | Back to comp.lang.python
Scope of a class..help??? lokeshkoppaka@gmail.com - 2013-05-23 02:51 -0700
Re: Scope of a class..help??? Chris Angelico <rosuav@gmail.com> - 2013-05-23 20:00 +1000
Re: Scope of a class..help??? Peter Otten <__peter__@web.de> - 2013-05-23 12:18 +0200
Re: Scope of a class..help??? lokeshkoppaka@gmail.com - 2013-05-23 03:23 -0700
Re: Scope of a class..help??? Chris Angelico <rosuav@gmail.com> - 2013-05-23 20:27 +1000
Re: Scope of a class..help??? lokeshkoppaka@gmail.com - 2013-05-23 03:25 -0700
Re: Scope of a class..help??? Chris Angelico <rosuav@gmail.com> - 2013-05-23 21:40 +1000
Re: Scope of a class..help??? Tim Roberts <timr@probo.com> - 2013-05-23 22:05 -0700
| From | lokeshkoppaka@gmail.com |
|---|---|
| Date | 2013-05-23 02:51 -0700 |
| Subject | Scope of a class..help??? |
| Message-ID | <913d7fc1-4608-4ff5-8b51-2ae2cd67781a@googlegroups.com> |
i had written the following code i am unable to create the instance of the class "Node" in the method "number_to_LinkedList" can any one help me how to do ??
and what is the error??
class Node:
def __init__(self, value=None):
self.value = value
self.next = None
def number_to_LinkedList(numbers):
pass
list_numbers = list(numbers)
head_node = Node() #unable to create the instance saying UnboundedLocal
head_node.value = list_numbers[0]
head_node.next = None
current_node = head_node
for i in range(1,len(list_numbers)):
new_node = Node()
new_node.value = list_numbers[i]
new_node.next = current_node
current_node = new_node
current_node.next = None
while Node:
print Node.data
Node = Node.next
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-05-23 20:00 +1000 |
| Message-ID | <mailman.2005.1369303610.3114.python-list@python.org> |
| In reply to | #45786 |
On Thu, May 23, 2013 at 7:51 PM, <lokeshkoppaka@gmail.com> wrote: > i had written the following code i am unable to create the instance of the class "Node" in the method "number_to_LinkedList" can any one help me how to do ?? > and what is the error?? It would really help if you post the actual exception and traceback. It's UnboundLocal, not Unbounded... and here's the problem: > def number_to_LinkedList(numbers): > head_node = Node() #unable to create the instance saying UnboundedLocal > while Node: > print Node.data > Node = Node.next You're assigning to Node. I think you mean to have some other local variable here, for the iteration at the end. Since you assign to the name Node inside the function (and don't have a global declaration), the name Node is local to the function. Python doesn't let you reference the global "prior to" shadowing it with the local, so you get this error. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-05-23 12:18 +0200 |
| Message-ID | <mailman.2006.1369304287.3114.python-list@python.org> |
| In reply to | #45786 |
lokeshkoppaka@gmail.com wrote:
> i had written the following code i am unable to create the instance of the
> class "Node" in the method "number_to_LinkedList" can any one help me how
> to do ?? and what is the error??
>
>
> class Node:
> def __init__(self, value=None):
> self.value = value
> self.next = None
>
>
>
> def number_to_LinkedList(numbers):
> pass
> list_numbers = list(numbers)
> head_node = Node() #unable to create the instance saying
> UnboundedLocal head_node.value = list_numbers[0]
> head_node.next = None
> current_node = head_node
> for i in range(1,len(list_numbers)):
> new_node = Node()
> new_node.value = list_numbers[i]
> new_node.next = current_node
> current_node = new_node
> current_node.next = None
> while Node:
> print Node.data
> Node = Node.next
You have to decide if you want to use a name in a function locally or to
access a global. Python treats names that are being assigned to anywhere in
a function as local throughout the whole function.
x = "global"
def f():
print x # ok, access global variable
x = "global"
def g():
x = "local"
print x # ok, accessing local variable
x = "global"
def h():
print x # error, accessing local variable that has not
# been assigned a value
x = "local"
[toc] | [prev] | [next] | [standalone]
| From | lokeshkoppaka@gmail.com |
|---|---|
| Date | 2013-05-23 03:23 -0700 |
| Message-ID | <110b4762-392d-4430-b1aa-6e8168ced9f3@googlegroups.com> |
| In reply to | #45786 |
Thanks Chris Angelico, i am new to python can you suggest me how to remove the error and solve it. so,how can i create an instance for "Node" in that function??,is, it not possible to create an instance in such a way?
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-05-23 20:27 +1000 |
| Message-ID | <mailman.2007.1369304850.3114.python-list@python.org> |
| In reply to | #45792 |
On Thu, May 23, 2013 at 8:23 PM, <lokeshkoppaka@gmail.com> wrote: > Thanks Chris Angelico, > i am new to python can you suggest me how to remove the error and solve it. > so,how can i create an instance for "Node" in that function??,is, it not possible to create an instance in such a way? The problem isn't the instantiation; the problem's further down, where you reuse the name "Node" to iterate over your list. Rename that to something else and you should be right. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | lokeshkoppaka@gmail.com |
|---|---|
| Date | 2013-05-23 03:25 -0700 |
| Message-ID | <6f05ea64-4a3a-4f92-a4c9-ade8bf7117c6@googlegroups.com> |
| In reply to | #45786 |
ok Peter Otten, but how to make a Class global??
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-05-23 21:40 +1000 |
| Message-ID | <mailman.2013.1369309217.3114.python-list@python.org> |
| In reply to | #45793 |
On Thu, May 23, 2013 at 8:25 PM, <lokeshkoppaka@gmail.com> wrote: > ok Peter Otten, > but how to make a Class global?? He gave some examples. It'd be helpful to quote some of his post, for context... and preferably, show some proof that you've understood it. You're starting a number of threads that look like you're trying to get us to do your homework. You need to demonstrate that you actually understand the material you're supposed to be learning. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Tim Roberts <timr@probo.com> |
|---|---|
| Date | 2013-05-23 22:05 -0700 |
| Message-ID | <oqstp81dmciehdkedt7b7i4bnlug2l1jg1@4ax.com> |
| In reply to | #45793 |
lokeshkoppaka@gmail.com wrote:
>
>ok Peter Otten,
>but how to make a Class global??
Your class IS global. I still don't think you understand what you did
wrong. You've tripped into a strange little quirk of scoping. Here is
your code with some unimportant lines removes.
class Node:
def __init__(self, value=None):
self.value = value
self.next = None
## OK, at this point, you have a global called "Node", which is a class.
def number_to_LinkedList(numbers):
pass
list_numbers = list(numbers)
head_node = Node()
# ...
current_node.next = None
while Node:
print Node.data
Python actually does a first scan through your function before it starts to
compile it. When it does the scan, it sees you use the name "Node" as a
local variable. At that point, it remembers that "in this function scope,
'Node' is a local name". Now, it codes to compile the class. When it
encounters Node() for the first time, it sees that "Node" is not defined
locally, even though it was supposed to be a local name.
You are assuming that the first Node() in that function will automatically
refer to the global class. It won't. The only way to solve this dilemma
is to change the name you use in the "while" loop.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web