Python Forum
Creating class with getters and setters.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating class with getters and setters.
#1
HI!
I'm trying to create a class with getters and setters in python mode because there are too many attributes to pass as argument in __init__ method.

So, here what i did:
class Tale:
	def __init__(self):
		self._title = ""
		self._author = ""
		self._category = ""
		self._date = None #it will be datetime object
		self._score = 0
		self._tale = ""
		self._numChars = 0

	@title.setter
	def title(self, title):
		self._title = title

	@author.setter
	def author(self, author):
		self._author = authorx

	@category.setter
	def category(self, category):
		self._category = category

	@date.setter
	def date(self,date):
		self._date = date

	@score.setter
	def score(self,score):
		self._score = score

	@tale.setter
	def tale(self,tale):
		self._tale = tale

	@numChars.setter
	def numChars(self,numChars):
		self._numChars = numChars

	@property
	def title(self):
	    return self._title
	
	@property
	def author(self):
	    return self._author
	
	@property
	def category(self):
	    return self._category
	
	@property
	def date(self):
	    return self._date
	
	@property
	def score(self):
	    return self._score
	
	@property
	def tale(self):
	    return self._tale
	
	@property
	def numChars(self):
	    return self._numChars
	
And that's the error message:
Quote:python test/main.py
Traceback (most recent call last):
File "test/main.py", line 4, in <module>
import tale
File "test/tale.py", line 1, in <module>
class Tale:
File "test/tale.py", line 11, in Tale
@title.setter
NameError: name 'title' is not defined

So I have two questions:
1- How can I fix this error?
2- What is the proper way to set the default value for those strings?
Reply
#2
https://python-forum.io/Thread-when-to-m...e+variable
Recommended Tutorials:
Reply
#3
Hi,

is there any reason why all attributes marked as internal use only? As you say, they normally should be sat via __init__, I guess these are normal "public" attributes?
Internal attributes shouldn't be accessed from the outside anyway.
In case you have a large number of attributes, just use pass a dict with value to the class.

In case you really want it this way, you need to turn each attribute into a property first, see https://docs.python.org/3/library/functi...l#property.

Example:

>>> class Foo:
...     def __init__(self):
...         self._title = None
...     @property
...     def title(self):
...         return self._title
...     @title.setter
...     def title(self, value):
...         self._title = value
...
>>> foo = Foo()
>>> foo.title = 'the python guru'
>>> foo.title
'the python guru'
>>>
I guess your mistake is that you define setters first, before turning the attributes to properties.

Except this, it's not technically necessary:

>>> class Bar:
...     def __init__(self):
...         self._title = None
...
>>> bar = Bar()
>>> bar._title = 'another python guru'
>>> bar._title
'another python guru'
>>>
Regards, noisefloor
Reply
#4
(Jun-18-2019, 02:36 AM)amandacstr Wrote: So I have two questions:
1- How can I fix this error?
2- What is the proper way to set the default value for those strings?

1) By defining things before referencing them (properties before setters). Or by just not using setters/getters.
2) By just giving it a value in the constructor.

Here's an example:
import datetime

class Tale:
	def __init__(self):
		self.title = "default title"
		self.author = "default author"
		self.category = ""
		self.date : datetime.date = None
		self.score = 0
		self.tale = ""
		self.numChars = 0

spam = Tale()
spam.title = "Shiny new thing"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating Email Message Class in Python QuavoJ 1 2,135 Jul-20-2020, 08:30 PM
Last Post: Yoriz
  Creating a script with a class have specific properties dvldgs05 13 5,779 Oct-15-2018, 08:54 PM
Last Post: dvldgs05

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020