I don't want to disturb you, but I don't understand how works the properties. I mean, using this simple code:

#!/usr/bin/env python2.6

def cosas(self, width):
return "cosas"

class Rect(object):
def __init__(self, width, height):
self.width = width
self.height = height
self.area = 0
print "Adios"

@property
def _area(self):
return self.width * self.height
#area = property(fget=_area)

@_area.setter
def _area(self, area):
self.__area = area

@property
def _width(self):
return self.__width

@_setWidth.setter
def _setWidth(self, setwidth):
self.__width = setwidth
print cosas(self,width)

python says: NameError: name '_setWidth' is not define
I've been trying to understand the reason, but I couldn't get it. Can someone explain me how works the properties?

The last question is related with methods inside a class. Taking a look at the following code, why python says:

class Rect(object):
def __init__(self, width, height):
self.width = width
self.height = height
self.area = 0
print "Adios"
def cosas2(self, width):
return "good"

def cosas(self, width):
return cosas2(width)

>>> import prueba
>>> p = prueba.Rect(8,9)
Adios
>>> p.cosas(8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "prueba.py", line 15, in cosas
return cosas2(width)
NameError: global name 'cosas2' is not defined

Thank you for your time.

In order to better help you, you'll need to help us (the other forum members). Clearly presenting your problem will allow us to quickly scan your post and answer your question.

In order to achieve this, you must use code tags when posting any code/traceback in this forum. By doing this, we get a clear distinction as to which part of your post is a message, and which part is code. I will demonstrate the code tags with an example of a NameError, so that hopefully you understand what it means:

>>> print(foo)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
NameError: name 'foo' is not defined
>>> foo = 'bar'
>>> print(foo)
bar
>>> 

As you can see, I hadn't yet defined foo. After doing so, the error disappeared. So in your case, you'll need to make sure that the object you're trying to access is available to the current scope of the program.

Edited 3 Years Ago by happygeek: fixed formatting

Thank you. Then, the code is this one:

class Rect(object):
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = 0
        print "Adios"
	def cosas2(self, width):
		return "good"
    
    def cosas(self, width):
		return cosas2(width)
	

	
    @property
    def _area(self):
        return self.width * self.height
    #area = property(fget=_area)
    
    @_area.setter
    def _area(self, area):
    	self.__area = area
    
    @property
    def _width(self):
    	self.__width = cosas()
        return self.__width
        
    @_setWidth.setter
    def _setWidth(self, setwidth):
        self.__width = setwidth
        print cosas(width)

and the error I got is:

>>> import prueba 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "prueba.py", line 5, in <module>
    class Rect(object):
  File "prueba.py", line 33, in Rect
    @_setWidth.setter
NameError: name '_setWidth' is not defined

The code have nothing interesting, I just took some examples from books and tried to get something, but it is always the same error. Therefore, my question is, what is the meaning of this error? Because I don't know what I should change in order to run the program.I think that the error must be in the property.

>>> import prueba 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "prueba.py", line 5, in <module>
    class Rect(object):
  File "prueba.py", line 33, in Rect
    @_setWidth.setter
NameError: name '_setWidth' is not defined

My question is, what is the meaning of this error? Because I don't know what I should change in order to run the program.I think that the error must be in the property.

The meaning is that _setWidth is not defined when you tried to access it. By attempting to decorate _setWidth with _setWidth.setter (what version of Python are you using?), you've asked Python to access _setWidth before you've defined it! That is why the NameError occurs.

This article has been dead for over six months. Start a new discussion instead.