|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Need urgent help please...
I need help with this code. It's code to insert and erase nodes in a list, and I think it's not correct:
•__slots__ = [‘__data’, ‘__next’, ‘__prev’] def__init__(self, initdata): self.__data = initdata self.__next= None self.__prev= None defgetData(self): returnself.__data defgetNext(self) returnself.__next defgetPrev(self) returnself.__prev defsetData(self, newdata): self.__data = newdata defsetNext(self, newnext) self.__next= newnext defsetPrev(self, newprev) self.__prev= newprev This is the code to erase the nodes: P.__prev.__next=P.__next P.__next.__prev=P.__prev and this is the code to insert nodes: nuevo= CDnode(d) nuevo.setNext(P.getNext()) nuevo.setPrev(P) P.getNext().setPrev(nuevo) P.setNext(nuevo) What part of the code to insert or erase do you think it's not OK? Thanks!! |
|
#2
|
|||||
|
|||||
|
Ok I'm going to try and help you here, but you don't make it easy.
First, when you paste code please maintain the proper indentation, make sure the code is exactly as it appears in your module(you have several missing colons) and use either the CODE or highlight="Python" tags. I'm assuming for the first section what you want is a class like this: Python Code:
The first thing I would say is that simple getters and setters like this aren't generally a good idea in python, its much simpler to reference the attributes directly. For example: instead of your getData method you would use something like: This however brings me onto the next problem, because you have implemented the attributes with the double-underscore (__) style name, they are awarded a level of privacy by python. So rather than being available as above, the actual code would need to be: Other than these points I'm really not sure what you are trying to achieve here, have a look at what I've posted and try asking again when you have a better idea of what you want to ask. EDIT: I see what you are trying to achieve with the __slots__ attribute, forgot what that did for a second there You should probably have a look at the property builtin function too. Looking again at your code I think thats what you were trying to do in the first place, both my previous points still stand though, in this instance there is no need for anything as complex as a getter/setter pair or even use of the property builtin and the __ variable names will still (afaik) prevent you from using these variables as properties. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Need urgent help please... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|