10 Replies - 1133 Views - Last Post: 15 September 2013 - 02:01 PM Rate Topic: -----

#1 gulaapan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-August 13

Class and object variables NameError

Posted 15 September 2013 - 10:32 AM

ok so im doing this (pdf)tutorial and im on objects and class variables and i can't get this to work, it's a simple
script not fancy at all, just for educational purposes but i want it to work properly and learn how to fix this problem...

it's an easy problem the errormessage says
NameError: name 'person' is not defined

but from what i know it has been defined, it's my class

here's the 3 first lines in my code....

class person:
    '''represents a person'''
    person.population = 0


...where i get this error.

when i ran it for the first time it was fine untill line 12 and 39 and there i got this error

Traceback (most recent call last):
File "C:/class_object_variables.py", line 39, in <module>
alex = person('Alex')


and this


File "C:/Users/class_object_variables.py", line 12, in __init__
population += 1
UnboundLocalError: local variable 'population' referenced before assignment


no idea what the line39 error is all about but the one on line 12 i may have a clue about, just don't know how i'll get it to work properly.... maybe i have to put person.population to get it work?

annoying idd...

i like this community very much nice and very very helpful people from all around the world connected with eachother, thats why i came here and not just googled it and i hope to get good useful answers.

try not to YELL out the answer directly, appriciated.

here comes the rest

class person:
    '''represents a person'''
    person.population = 0

    def __init__(self, name):
        '''initialize the persons data.'''
        self.name = name
        print '(Initializing %s)' % self.name

        #when this person is created, he/she
        #adds to the population
        person.population += 1

    def __del__(self):
        '''i am dying'''
        print '%s says bye...' % self.name

        personpopulation -= 1

        if person.population == 0:
            print 'i am the last one.'

        else:
            print 'there are still %d people left.' % person.populatio
            
    def sayHi(self):
        '''Greeting by the person'''
        print 'Hi! My name is %s' % self.name

    def howMany(self):
        '''prints the current population'''
        if person.population == 1:
            print 'i am the only person here.'

        else:
            print 'we have %d persons here.' % person.population


alex = person('Alex')
alex.sayHi()
alex.howMany()

kalam = person('lisa')
kalam.sayHi()
kalam.howMany()

alex.sayHi()
alex.howMany()



i'm very new to python just so you know ;)/>

have a nice day folks!

peace

Is This A Good Question/Topic? 0
  • +

Replies To: Class and object variables NameError

#2 gulaapan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-August 13

Re: Class and object variables NameError

Posted 15 September 2013 - 10:51 AM

i'm using python 2.7 and i think this pdf is for 2.3 or higher

nevermind guys, i solved it ^^/> just a minor mistake.. here's the right code

class person:
    '''represents a person'''
    population = 0

    def __init__(self, name):
        '''initialize the persons data.'''
        self.name = name
        print '(Initializing %s)' % self.name

        #when this person is created, he/she
        #adds to the population
        person.population += 1

    def __del__(self):
        '''i am dying'''
        print '%s says bye...' % self.name

        person.population -= 1

        if person.population == 0:
            print 'i am the last one.'

        else:
            print 'there are still %d people left.' % person.populatio
            
    def sayHi(self):
        '''Greeting by the person'''
        print 'Hi! My name is %s' % self.name

    def howMany(self):
        '''prints the current population'''
        if person.population == 1:
            print 'i am the only person here.'

        else:
            print 'we have %d persons here.' % person.population


alex = person('Alex')
alex.sayHi()
alex.howMany()

kalam = person('lisa')
kalam.sayHi()
kalam.howMany()

alex.sayHi()
alex.howMany()

notice this

class person:
    '''represents a person'''
    population = 0

This post has been edited by andrewsw: 15 September 2013 - 10:53 AM
Reason for edit:: code tags

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Class and object variables NameError

Posted 15 September 2013 - 10:56 AM

Glad you sorted it.

(You also have the last letter missing from 'populatio' but I assume this is just a mistake in pasting.)

BTW I recommend that you learn Python 3, unless you have a specific need to learn Python 2.x. Or, at least, use brackets with all your print statements (a function in Python 3):

print("This will work in Python 3")

What's new in Python 3

This post has been edited by andrewsw: 15 September 2013 - 11:00 AM

Was This Post Helpful? 0
  • +
  • -

#4 gulaapan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-August 13

Re: Class and object variables NameError

Posted 15 September 2013 - 11:09 AM

mistake in pasting ^^

but about python 3... i've heard from many that you should use python 2x and not python 3 so that's what i've been doing . are there any specific reasons to learn py3?

This post has been edited by andrewsw: 15 September 2013 - 11:12 AM
Reason for edit:: Removed previous quote

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Class and object variables NameError

Posted 15 September 2013 - 11:18 AM

Who told you that? Python 3 is the modern version (since 2009) and all new code should, preferably, be written with it. (Learning Python 2.x might be sensible if you knew you had to work with legacy code.)

However, I appreciate that a lot of tutorials will still use Python 2.x. Nevertheless, if you read through that article I linked to, it is easy to write code in Python 2 that will still run in Python 3. Essentially,

print("Always write print followed by brackets")
something = input("Use 'input' rather than 'raw_input'")

Was This Post Helpful? 0
  • +
  • -

#6 gulaapan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-August 13

Re: Class and object variables NameError

Posted 15 September 2013 - 11:22 AM

if there's any interest in this code and you want to add the __del__ object

just type i.e lisa.__del__() to the variables

alex = person('Alex')
alex.sayHi()
alex.howMany()

lisa = person('lisa')
lisa.sayHi()
lisa.howMany()

lisa.__del__()

alex.sayHi()
alex.howMany()


peace

View Postandrewsw, on 15 September 2013 - 11:18 AM, said:

Who told you that? Python 3 is the modern version (since 2009) and all new code should, preferably, be written with it. (Learning Python 2.x might be sensible if you knew you had to work with legacy code.)

However, I appreciate that a lot of tutorials will still use Python 2.x. Nevertheless, if you read through that article I linked to, it is easy to write code in Python 2 that will still run in Python 3. Essentially,

print("Always write print followed by brackets")
something = input("Use 'input' rather than 'raw_input'")


True i should keep myself up to date, maybe i should consider change to python3. ye thanks for the link, i read thrue it quick and there are not as many differences as i thought. i will defenetly take a deeper look at it.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Class and object variables NameError

Posted 15 September 2013 - 11:25 AM

You should not call __del__ directly, just use:

del lisa

which will, in turn (eventually), trigger the __del__ (magic) method. (Quite often there is no need to specifically delete an object - it just disappears when it falls out of scope.)

This post has been edited by andrewsw: 15 September 2013 - 11:31 AM

Was This Post Helpful? 1
  • +
  • -

#8 gulaapan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-August 13

Re: Class and object variables NameError

Posted 15 September 2013 - 11:28 AM

View Postandrewsw, on 15 September 2013 - 11:25 AM, said:

You should not call __del__ directly, just use:

del lisa

which will, in turn, trigger the __del__ (magic) method.


Thank you for that! this is why i come here.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Class and object variables NameError

Posted 15 September 2013 - 11:35 AM

This tutorialspoint page is also in Python 2, but it looks like a good one page summary of OOP in Python. It's almost entirely consistent with Python 3, apart from the print "statements".

This post has been edited by andrewsw: 15 September 2013 - 11:41 AM

Was This Post Helpful? 1
  • +
  • -

#10 gulaapan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-August 13

Re: Class and object variables NameError

Posted 15 September 2013 - 01:55 PM

View Postandrewsw, on 15 September 2013 - 11:35 AM, said:

This tutorialspoint page is also in Python 2, but it looks like a good one page summary of OOP in Python. It's almost entirely consistent with Python 3, apart from the print "statements".



nice took a quick look at it and it's what i'm learning right now.

so this works fine in python 3 except the print statements?

are the print statement a function in py3?
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Class and object variables NameError

Posted 15 September 2013 - 02:01 PM

View Postgulaapan, on 15 September 2013 - 08:55 PM, said:

are the print statement a function in py3?

Yes, as detailed in the page I linked. Basically, just ensure you use brackets

print("all the time")

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1