Reputation: 187
I have the following code, where somehow the class is not honoring the self. name., when I access the class variable in a function, it is complaining that the self.variable is not a global variable. Any ideas why?
from sqlalchemy import Column, ForeignKey, Integer , String, Float, DateTime
from sqlalchemy.ext.declarative import declarative_base
Base=declarative_base()
class Designs(Base):
__tablename__='designs'
design_name=Column(String(80),nullable=False,primary_key=True)
@property
def serialize(self):
return{
'design_name': self.design_name,
}
When I access the class, design_name is there, but somehow python is complaining it is not declared as global? Any Ideas?
let's say temp declared with the Designs class, and is filled with value for design_name
print temp.design_name
print temp.serialize()
xpc_fp <----- print i.design_name works
serialize command does not work** and gives the following error:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "database_setup.py", line 149, in serialize
'design_name': self.design_name,
NameError: global name 'design_name' is not defined
Upvotes: 1
Views: 491
Reputation: 2962
You define serialize as a property - so you should treat it as a property.
from sqlalchemy import Column, ForeignKey, Integer , String, Float, DateTime
from sqlalchemy.ext.declarative import declarative_base
Base=declarative_base()
class Designs(Base):
__tablename__='designs'
design_name=Column(String(80),nullable=False,primary_key=True)
@property
def serialize(self):
return{
'design_name': self.design_name,
}
temp = Designs(design_name='HELLO THERE')
print(temp.design_name)
print(temp.serialize)
so drop the parenthesis from the .serialize call
output:
HELLO THERE
{'design_name': 'HELLO THERE'}
Upvotes: 1