Python Forum
"can't set attribute" on class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"can't set attribute" on class
#1
I have a class like so:
class CustomLevel(BaseLevel):

	def __init__(self, **data: dict) -> None:
		self.data = data

	@classmethod
	def fromData(cls, data: Union[dict, str]) -> CustomLevel:
		if(isinstance(data, str)):
			pass
		return cls(
			data=data.get("level_data", ""),
			name=data.get("name", "Unknown"),
			id=data.get("id", ""),
			creator=data.get("creator", "Unknown")
		)

	@property
	def data(self) -> str:
		return self.data.get("level_data", "")

	@property
	def name(self) -> str:
		return self.data.get("name", "Unknown")

	@property
	def id(self) -> str:
		return self.data.get("id", "")
		
	@property
	def creator(self) -> str:
		return self.data.get("creator", "Unknown")

	@classmethod
	def getParams(self) -> URLParameters:
		return URLParameters({
			"gameVersion": "21",
			"binaryVersion": "35",
			"gdw": "0",
			"levelID": self.id,
			"secret": "Wmfd2893gb7" #this is not a secret its the same for everyone
		})
It inherits a base class which is just a class with the exact same functions but with no code within them.
To use this class, I go:
level = CustomLevel.fromData({"id": id})
By doing this, it throws:
Error:
--- <exception caught here> --- File "/Users/everyone/.pyenv/versions/3.8.0/lib/python3.8/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks result = g.send(result) File "/Users/everyone/Desktop/gdreplacer/simple_server.py", line 31, in something print(self.locations[name](data)) File "/Users/everyone/Desktop/gdreplacer/level.py", line 109, in replace level = CustomLevel.fromData({"id": constants.ARGS.payload_id}) File "/Users/everyone/Desktop/gdreplacer/level.py", line 57, in fromData return cls( File "/Users/everyone/Desktop/gdreplacer/level.py", line 51, in __init__ self.data = data builtins.AttributeError: can't set attribute
It clearly doesn't like me trying to set the "data" attribute. If I print the contents of data before trying to set it, I get this:
Output:
{'data': '', 'name': 'Unknown', 'id': '88685', 'creator': 'Unknown'}
which is correct.

How come it is not letting me set an attribute?
Reply
#2
The problem is that you have a read-only @property named data. The best solution is to rename the attribute. You could use _data or datadict for example.
Reply
#3
(Aug-22-2020, 07:53 PM)Gribouillis Wrote: The problem is that you have a read-only @property named data. The best solution is to rename the attribute. You could use _data or datadict for example.
Ah that was it - thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Initiating an attribute in a class __init__: question billykid999 8 1,247 May-02-2023, 09:09 PM
Last Post: billykid999
  class, attribute and method Frankduc 9 2,379 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Python generics: How to infer generic type from class attribute? Thoufak 0 2,769 Apr-25-2021, 09:31 AM
Last Post: Thoufak
  AttributeError class object has no attribute list object scttfnch 5 3,339 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  Class has no attribute <obj-name> arunprasanth 2 4,497 Mar-18-2019, 02:08 PM
Last Post: arunprasanth
  Class object instance. Link instance attribute to class. Can it be done easier. Windspar 7 4,069 Dec-03-2018, 11:16 PM
Last Post: Windspar
  AttributeError: 'NoneType' object has no attribute 'n' in list of class objects jdrp 4 5,694 Jun-19-2018, 02:44 PM
Last Post: jdrp
  Empty attribute class dictionary after saving it in a class object dictionary 3dimensions 6 4,792 May-20-2018, 01:57 PM
Last Post: 3dimensions
  setting base class attribute bb8 1 2,485 Feb-13-2018, 06:37 PM
Last Post: nilamo
  Class attribute not recognized\working J125 1 5,292 Dec-19-2016, 01:05 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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