ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm attempting to learn Python on my own,so, I got a piece of software I had written in C#, and attempted to re-write it in Python. Given the following class I have a few questions:
C#
Code:
sealed class Message
{
private int messageID;
private string message;
private ConcurrentBag <Employee> messageFor;
private Person messageFrom;
private string calltype;
private string time;
public Message(int iden,string message, Person messageFrom, string calltype,string time)
{
this.MessageIdentification = iden;
this.messageFor = new ConcurrentBag<Employee>();
this.Note = message;
this.MessageFrom = messageFrom;
this.CallType = calltype;
this.MessageTime = time;
}
public ICollection<Employee> ReturnMessageFor
{
get
{
return messageFor.ToArray();
}
}
1. In my class I have a thread-safe collection called messageFor, is there an equivalent in Python? If so, how do I implement it in a python class?
2. I also have a getter for my thread-safe collection? How would I go about doing the same in Python?
3. Does Python have an EqualsTo method to test equality between objects? Or the
equivalent of this in Python?
]I'm trying to learn Python on my own, so I got a copy of class I'd written in C# and attempt to write it in Python. I've got two questions:
1. How would you write this C# Class in python? My problem is when it comes to the thread-safe collection messageFor. Does Python have thread-safe collections? If so, how would you implement it as shown above?
2. Does Python have an equalsTo method to test for equality between Objects?
You're starting out with a very involved example. Instead first write something small in Python and grow the concepts to include much of what is represented within your example class. Or you can submit what you have to the python interpreter program you have and see where it complains and then correct the syntax piece by piece. My style here would be to start small and grow my class definition until I got an equivalent. This way whatever concepts needed to be re-designed, I learned them as I went along.
Most persons here will offer various suggestions, but the overall intent is that you make an attempt first rather than copy in a bunch of code an ask people to write an equivalent for you.
Thanks for trimming your code down to the parts you actually need help with. Unfortunately, my reply was to the full version.
I see you've added this:
Quote:
4. Can you make it sealed, so that no one can inherit from it?
The real answer is no.
A Google search for "python sealed class" got me a way to do it, if you really, really want to, but really, the answer is no. That isn't done in Python. Anyway, if you still want to do it, this will work:
Generally speaking, though, the final answer will be: "no."
The C# language is a compiled language that is designed to do things that only such languages do. (For example, if you wanted to write a language interpreter, C# would be a fine language to do it in. I know this because I've done it...)
Python, on the other hand, is designed to be an interpreted environment. While it "supports objects and classes," the scope of its implementation is not designed to be so extensive ... nor would it necessarily be appropriate (or perhaps, even implementable) for it so to be.
As a compiler, C# can generate code to do anything, without affecting itself. When the generated program runs, it's completely free to set up and to maintain any environment it wants. As an interpreter, the Python interpreter is "the actual program that the computer is running," and this doesn't change from Python-run to run, even though the bytecode/source-code being interpreted does.
Last edited by sundialsvcs; 07-29-2015 at 08:58 AM.
Thanks for trimming your code down to the parts you actually need help with. Unfortunately, my reply was to the full version.
I see you've added this:
The real answer is no.
A Google search for "python sealed class" got me a way to do it, if you really, really want to, but really, the answer is no. That isn't done in Python. Anyway, if you still want to do it, this will work:
Thanks for trimming your code down to the parts you actually need help with. Unfortunately, my reply was to the full version.
I see you've added this:
The real answer is no.
A Google search for "python sealed class" got me a way to do it, if you really, really want to, but really, the answer is no. That isn't done in Python. Anyway, if you still want to do it, this will work:
I believe I've answered everything. Let me know if there's anything that was missed.
I posted this same question on StackOverFlow, however, no one did what you were able to do, again Thanks. However, at the bottom the last comment, says
Quote:
Don't return False from __eq__ because of the rhs being the wrong type: return NotImplemented instead. Think of it as three value logic: 'yes', 'no' or 'I don't know' - and if you don't know, python might be able to ask the other object if it knows
Do you agree, and how would you implement what the poster was talking about?
Do you agree, and how would you implement what the poster was talking about?
I don't agree in this case, because the goal is to do the same thing as your C# code, and your C# code returns False if you're comparing the "wrong type".
I don't agree in this case, because the goal is to do the same thing as your C# code, and your C# code returns False if you're comparing the "wrong type".
Learning Python was difficult for me, actually. There was one book that did click with me (I think it was by that guy who chose to disappear from the Internet after writing it), but I can't really recall it right now.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.