|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
Task manager is not reliable, no. The OS typically reserves a larger chunk of memory for the application than what it actually needs. If the required memory suddenly drops a bunch, the OS still doesn't take away the reserved memory immediately, but might wait until memory pressure rises because of other applications.
Task manager doesn't show exactly how much memory an application uses. I'm not sure if it's the reserved memory either, but that's more likely.
C++ delete releases the memory immediately, but this isn't reflected in task manager for the above reasons. The free memory chunk might also very well between two other used memory chunks and thus still be considered as used from the OS' point of view.
Long story short: if you want any sort of reliable memory usage stats, use a profiler.
|
In python3, how do i create objects with an instance name and multiple arguments from a list? I have an example here. I need a way to initialize in a for loop with variable names that allow me to access the instance later.
class fruit(): def __init__(self, color, weight): self.color = color self.weight = weight def scale(self): print(f"{self.weight} kg") # i can do strawberry = fruit("red", 10) strawberry.scale() # output "10 kg" # but i cant do fruitlist = [ ["cherry", "red", 5], ["banana", "yellow", 10], ["blueberry", "blue", 15], ] for i in range(len(fruitlist)): fruitname = fruitlist[i][0] fruitname = fruit(fruitlist[i][1], fruitlist[i][2]) cherry.scale() # output "undefined name cherry"
|
On January 02 2018 18:02 graNite wrote:In python3, how do i create objects with an instance name and multiple arguments from a list? I have an example here. I need a way to initialize in a for loop with variable names that allow me to access the instance later. class fruit(): def __init__(self, color, weight): self.color = color self.weight = weight def scale(self): print(f"{self.weight} kg") # i can do strawberry = fruit("red", 10) strawberry.scale() # output "10 kg" # but i cant do fruitlist = [ ["cherry", "red", 5], ["banana", "yellow", 10], ["blueberry", "blue", 15], ] for i in range(len(fruitlist)): fruitname = fruitlist[i][0] fruitname = fruit(fruitlist[i][1], fruitlist[i][2]) <<<<< cherry.scale() <<<<<< # output "undefined name cherry"
well... you name the variable where you store your fruit "fruitname"
so either write "cherry = fruit(fruitlist[i][1], fruitlist[i][2])" or "fruitname.scale()"
|
On January 02 2018 18:28 mahrgell wrote: well... you name the variable where you store your fruit "fruitname"
so either write "cherry = fruit(fruitlist[i][1], fruitlist[i][2])" or "fruitname.scale()"
yees, but that every fruit is named cherry or i weigh when in the for loop, but i cant access them all like
cherry.scale() banana.scale()
|
On January 02 2018 18:28 mahrgell wrote:Show nested quote +On January 02 2018 18:02 graNite wrote:In python3, how do i create objects with an instance name and multiple arguments from a list? I have an example here. I need a way to initialize in a for loop with variable names that allow me to access the instance later. class fruit(): def __init__(self, color, weight): self.color = color self.weight = weight def scale(self): print(f"{self.weight} kg") # i can do strawberry = fruit("red", 10) strawberry.scale() # output "10 kg" # but i cant do fruitlist = [ ["cherry", "red", 5], ["banana", "yellow", 10], ["blueberry", "blue", 15], ] for i in range(len(fruitlist)): fruitname = fruitlist[i][0] fruitname = fruit(fruitlist[i][1], fruitlist[i][2]) <<<<< cherry.scale() <<<<<< # output "undefined name cherry"
well... you name the variable where you store your fruit "fruitname" so either write "cherry = fruit(fruitlist[i][1], fruitlist[i][2])" or "fruitname.scale()"
Or you'd have to do some funny business with exec, which seems completely bizar in this case.
In most cases, you really don't want to be doing this, as it slows down your program and is almost impossible to debug or understand at a later date/by someone else. But if you insist, you would do:
for i in range(len(fruitlist)): exec(fruitlist[i][0] + " = fruit('" + fruitlist[i][1] + "', " + str(fruitlist[i][2]) + ")")
This is somewhat horrific coding tho, and you'd better have an incredibly good reason for needing to do it this way.
|
And what would be an alternative? I dont have to use this list of lists, I just want to be able to the the objects to access them later and initialize with certain values.
|
On January 02 2018 18:42 graNite wrote: And what would be an alternative? I dont have to use this list of lists, I just want to be able to the the objects to access them later and initialize with certain values. Something like:
fruitlist = [ fruit(x, y) for (x,y) in [ ("red", 5), ("yellow", 10), ("blue", 15), ] ]
E: or if the names are important to you, a dict.
|
On January 02 2018 19:32 Acrofales wrote:
E: or if the names are important to you, a dict.
can you pls show me what that dict would look like? the names are important, otherwise I could just use the code i provided in the first post
|
class fruit(): def __init__(self, color, weight): self.color = color self.weight = weight def scale(self): print(f"{self.weight} kg")
cherry = fruit('red', 5) banana = fruit('yellow', 10) blueberry = fruit('blue', 15)
fruitlist = [cherry, banana, blueberry]
for i in fruitlist: print(i.color, i.weight)
I am not sure if that is what You are looking for but in Your first example You didnt actually create objects of class fruit. You just created a list of list. If You want to create obejct of a class You need to call that class explicitly.
To create object of an class froma list You would need another method. And it would be much easier if You actualy created an argument called say name. For example:
class fruit(): def __init__(self, name, color, weight): self.color = color self.weight = weight self.name = name def scale(self): print(f"{self.weight} kg")
fruitlist = [ ["cherry", "red", 5], ["banana", "yellow", 10], ["blueberry", "blue", 15], ] fruit_class_holder = []
def construct_instance_of_fruit(list): for i in list: fruit_name = i[0] fruit_color = i[1] fruit_weight = i[2] fruit_as_a_class = fruit(fruit_name, fruit_color, fruit_weight) fruit_class_holder.append(fruit_as_a_class)
construct_instance_of_fruit(fruitlist)
for i in fruit_class_holder: print(i.name, i.color, i.weight)
Notice i added argument "name" to Your initial class.
The output is:
cherry red 5 banana yellow 10 blueberry blue 15
|
On January 02 2018 21:46 graNite wrote:Show nested quote +On January 02 2018 19:32 Acrofales wrote:
E: or if the names are important to you, a dict. can you pls show me what that dict would look like? the names are important, otherwise I could just use the code i provided in the first post Okay, in that case you need to understand a bit about the logic of programming:
Variable names are important for developers to understand what is happening in the code, but mean absolutely NOTHING at runtime. Everything that is important at runtime should be stored in data structures. Whether that is individual variables, a list, a dict, or some other data structure. In the example silvanel gave, he has created a variable name in the fruit class, and can then simply initialize all of the fruits and store them in a list (as in my exerpt, but adding a field 'name').
You should always be aware of that distinction: if a minimizer or obfuscator garbles up your code, it should work just as it did before, just be illegible for humans. Very rarely you will indeed encounter a situation where you need reflection and have to recover variable names at runtime, but that is a rather advanced usecase, and even then often a kludge and not a proper way of going about doing what needs doing.
As for an example with a list:
fruitdict = {} for i in range(len(fruitlist)): fruitdict[fruitlist[i][0]] = fruit(fruitlist[i][1], fruitlist[i][2])
fruitdict["banana"].scale()
This could be done with list comprehension as well (as in my earlier exerpt).
|
On January 02 2018 22:46 Silvanel wrote:+ Show Spoiler + class fruit(): def __init__(self, color, weight): self.color = color self.weight = weight def scale(self): print(f"{self.weight} kg")
cherry = fruit('red', 5) banana = fruit('yellow', 10) blueberry = fruit('blue', 15)
fruitlist = [cherry, banana, blueberry]
for i in fruitlist: print(i.color, i.weight)
I am not sure if that is what You are looking for but in Your first example You didnt actually create objects of class fruit. You just created a list of list. If You want to create obejct of a class You need to call that class explicitly. i tried to do that here:
for i in range(len(fruitlist)): fruitname = fruitlist[i][0] fruitname = fruit(fruitlist[i][1], fruitlist[i][2])
To create object of an class froma list You would need another method. And it would be much easier if You actualy created an argument called say name. For example: + Show Spoiler + class fruit(): def __init__(self, name, color, weight): self.color = color self.weight = weight self.name = name def scale(self): print(f"{self.weight} kg")
fruitlist = [ ["cherry", "red", 5], ["banana", "yellow", 10], ["blueberry", "blue", 15], ] fruit_class_holder = []
def construct_instance_of_fruit(list): for i in list: fruit_name = i[0] fruit_color = i[1] fruit_weight = i[2] fruit_as_a_class = fruit(fruit_name, fruit_color, fruit_weight) fruit_class_holder.append(fruit_as_a_class)
construct_instance_of_fruit(fruitlist)
for i in fruit_class_holder: print(i.name, i.color, i.weight)
Notice i added argument "name" to Your initial class.
Yes, but i still cant do cherry.scale() to access the scale method directly. that is what i want to do.
|
To use it like that You need a variable called cherry which stores object of a class fruit. Which You dont have (as You dont create it at any point in Your code).
However a slight modification of code i posted above).
for i in fruit_class_holder: print(i.scale())
Will work and give output.
5 kg None 10 kg None 15 kg None
|
On January 02 2018 23:19 graNite wrote:Show nested quote +On January 02 2018 22:46 Silvanel wrote:+ Show Spoiler + class fruit(): def __init__(self, color, weight): self.color = color self.weight = weight def scale(self): print(f"{self.weight} kg")
cherry = fruit('red', 5) banana = fruit('yellow', 10) blueberry = fruit('blue', 15)
fruitlist = [cherry, banana, blueberry]
for i in fruitlist: print(i.color, i.weight)
I am not sure if that is what You are looking for but in Your first example You didnt actually create objects of class fruit. You just created a list of list. If You want to create obejct of a class You need to call that class explicitly. i tried to do that here: for i in range(len(fruitlist)): fruitname = fruitlist[i][0] fruitname = fruit(fruitlist[i][1], fruitlist[i][2])
Show nested quote +To create object of an class froma list You would need another method. And it would be much easier if You actualy created an argument called say name. For example: + Show Spoiler + class fruit(): def __init__(self, name, color, weight): self.color = color self.weight = weight self.name = name def scale(self): print(f"{self.weight} kg")
fruitlist = [ ["cherry", "red", 5], ["banana", "yellow", 10], ["blueberry", "blue", 15], ] fruit_class_holder = []
def construct_instance_of_fruit(list): for i in list: fruit_name = i[0] fruit_color = i[1] fruit_weight = i[2] fruit_as_a_class = fruit(fruit_name, fruit_color, fruit_weight) fruit_class_holder.append(fruit_as_a_class)
construct_instance_of_fruit(fruitlist)
for i in fruit_class_holder: print(i.name, i.color, i.weight)
Notice i added argument "name" to Your initial class. Yes, but i still cant do cherry.scale() to access the scale method directly. that is what i want to do.
Here's how I would do it quickly. You store each of the fruit in a dictionary "my_fruit", you can access "the_fruit" using my_fruit[the_fruit].
fruit.py 1 class fruit(): 2 def __init__(self, name, color, weight): 3 self.color = color 4 self.weight = weight 5 self.name = name 6 7 def scale(self): 8 print(f"{self.weight} kg") 9 10 f1 = fruit("cherry", "red", 5) 11 f1.scale() 12 13 fruitlist = [ 14 ["cherry", "red", 5], 15 ["banana", "yellow", 10], 16 ["blueberry", "blue", 15] 17 ] 18 19 my_fruits = dict() 20 for i in fruitlist: 21 my_fruits[i[0]] = fruit(i[0], i[1], i[2]) 22 23 for key, vals in my_fruits.items(): 24 vals.scale() 25 26 my_fruits["cherry"].scale()
$ python fruit.py 5 kg 5 kg 10 kg 15 kg 5 kg
|
Does anyone have a good recommendation for a C++ textbook (or maybe C, I haven't decided yet)?
I've taken 3 Java classes so far (an intro class, object oriented, and data structures), and I'm taking Systems I next semester which is in C and C++. I decided I wanted to get a bit of a head start on it just for fun over break.
So far these seem decent, anything else you guys would recommend I check out?
- Effective C++: 55 Specific Ways to Improve Your Programs and Designs - Accelerated C++: Practical Programming by Example
|
On January 03 2018 06:59 Varanice wrote: Does anyone have a good recommendation for a C++ textbook (or maybe C, I haven't decided yet)?
I've taken 3 Java classes so far (an intro class, object oriented, and data structures), and I'm taking Systems I next semester which is in C and C++. I decided I wanted to get a bit of a head start on it just for fun over break.
So far these seem decent, anything else you guys would recommend I check out?
- Effective C++: 55 Specific Ways to Improve Your Programs and Designs - Accelerated C++: Practical Programming by Example
You can start off with K&R C. This won't overwhelm you with too much knowledge and will be a great foundation for everything else.
https://en.wikipedia.org/wiki/The_C_Programming_Language
After that you can go with C++
https://en.wikipedia.org/wiki/The_C++_Programming_Language
Edit:
Also, in relation to Java, you should really read GOOS: http://www.growing-object-oriented-software.com/
This shows you some of the very useful stuff with OOP that is used everywhere at work but they don't teach you in the academia.
|
Does anyone know any good larger/more active programming communities like this? While y'all are great this is unfortunately a single thread on a starcraft forum. Are there any forums(or whatever else) for discussion, code, etc. that are this high quality? SO doesn't count because it's too issue-based.
|
On January 03 2018 14:01 WarSame wrote: Does anyone know any good larger/more active programming communities like this? While y'all are great this is unfortunately a single thread on a starcraft forum. Are there any forums(or whatever else) for discussion, code, etc. that are this high quality? SO doesn't count because it's too issue-based. for all languages or just a specific one?
|
On January 03 2018 07:24 Manit0u wrote:Show nested quote +On January 03 2018 06:59 Varanice wrote: Does anyone have a good recommendation for a C++ textbook (or maybe C, I haven't decided yet)?
I've taken 3 Java classes so far (an intro class, object oriented, and data structures), and I'm taking Systems I next semester which is in C and C++. I decided I wanted to get a bit of a head start on it just for fun over break.
So far these seem decent, anything else you guys would recommend I check out?
- Effective C++: 55 Specific Ways to Improve Your Programs and Designs - Accelerated C++: Practical Programming by Example You can start off with K&R C. This won't overwhelm you with too much knowledge and will be a great foundation for everything else. https://en.wikipedia.org/wiki/The_C_Programming_LanguageAfter that you can go with C++ https://en.wikipedia.org/wiki/The_C++_Programming_Language I heavily disagree with starting with K&R C then going into c++, the language is significantly different and will serve you no real purpose.
Depending on how intelligent you're feeling start with one of these two: c++ primer 5th edition (NOT primer plus) - the second chapter is probably the most complex of all the chapters, giving you a nice intro into pointers, pointer arithmetic, auto and its results, and const, before combining them. If you don't have any programming experience I'd avoid starting with this and instead choose
programming principles and practice using c++ - pretty much the perfect beginner programming book.
On January 03 2018 14:01 WarSame wrote: Does anyone know any good larger/more active programming communities like this? While y'all are great this is unfortunately a single thread on a starcraft forum. Are there any forums(or whatever else) for discussion, code, etc. that are this high quality? SO doesn't count because it's too issue-based. A lot of the individual sub reddits are good, depending on the language, hackernews is also pretty decent, though it's turning a bit redditish for my liking.
I despise /r/programming and loath /r/technology though.
|
Hyrule18982 Posts
On January 03 2018 14:01 WarSame wrote: Does anyone know any good larger/more active programming communities like this? While y'all are great this is unfortunately a single thread on a starcraft forum. Are there any forums(or whatever else) for discussion, code, etc. that are this high quality? SO doesn't count because it's too issue-based. Only ones I can think of are various subreddits (like /r/programming)
|
On January 03 2018 14:01 WarSame wrote: Does anyone know any good larger/more active programming communities like this? While y'all are great this is unfortunately a single thread on a starcraft forum. Are there any forums(or whatever else) for discussion, code, etc. that are this high quality? SO doesn't count because it's too issue-based.
There are loads for specific tools, IDEs and languages, but not a whole lot for everything. Stackoverflow comes to mind, but that's more of a "I have this very specific issue plz hepl!" type of place rather than a forum about discussions.
|
|
|
|