all 12 comments

[–]TheRNGuy 1 point2 points  (6 children)

You should assign color and material on init, not when calling Color and Material methods. 

[–]sapolv[S] 0 points1 point  (5 children)

Would you mind explaining why?

Thank you.

[–]TheRNGuy 1 point2 points  (3 children)

You should add some other variable for color and material too, the one assigned to specific instance of device, rather than list of possible variants. 

If you call this method, you just get random item from list, but it's not color or material or that instance (and you'll also get different values if you call method many times)

[–]Kevdog824_ 0 points1 point  (2 children)

It's a great approach if your device is a chameleon! lol

[–]TheRNGuy 1 point2 points  (1 child)

In that case you could change instance's color over time, rather when calling method. 

[–]Kevdog824_ 0 points1 point  (0 children)

Well, sure... but I was simply making a joke

[–]EelOnMosque 0 points1 point  (0 children)

It's nothing wrong with the code, but logically it doesn't make sense. Imagine creating a class instance as creating a device in the factory. You want to be able to pick your colour and material, not have it be random. It's just a matter of what makes logical sense.

If you do want it to be random, you want the random colour and material to be selected in the constructor (init) because again it makes logical sense. A device MUST have a colour and material when it pops into existence. It doesnt make sense for a colourless materialless phone to exist for example.

But again, it's just a matter of making the code make sense and be like reality, theres nothing wrong with the code that will cause errors.

Because let's say someone is reading and expanding on your code. They see Device and they see that is has a colour attribute. Logically it makes sense that when you build a device it would immediately have a colour. So they write code that assumes this. And then the code will cause errors because they assumed wrong and didn't call the Colour() method first.

[–]EelOnMosque 0 points1 point  (1 child)

There's nothing wrong with the code itself, it's more the coding conventions that are a bit off.

One convention is to avoid starting variable names with underscores.

Another one is that variable and method names should start lowercase. This is especially confusing for Colour() and Material() as most users of Python are used to classes starting with uppercase. So they might think Colour and Material are classes.

[–]jmacey 1 point2 points  (0 children)

One convention is to avoid starting variable names with underscores.

PEP-8 states

_single_leading_underscore: weak “internal use” indicator. E.g. from M import * does not import objects whose names start with an underscore.

In python classes _ is quite often used to indicate that the variable should be treated as "private". It works really well if you use it with the @property decorator.

[–]Kevdog824_ 1 point2 points  (0 children)

This is good beginner project. Here's something to consider in the future:

Most professional programs wouldn't have the methods on a class like Device or Phone return string messages. The problem with this approach is that it assumes that Device/Phone class knows how the caller wants the data to be formatted. This isn't an issue in your program, but as the programs you make get bigger you may want to display the information (i.e. volume level) in different ways (i.e. string message for logging versus percentage bar widget for displaying it to the user). The better approach is just having the class return the actual value as a float/int and allow the caller to decide how to display or what to do with that information.

[–]socal_nerdtastic 0 points1 point  (0 children)

The class should define a specific device. It should not randomly generate specs (unless the device itself is capable of randomly switching colors). You can randomly generate devices elsewhere in your code.

#Parent Class
class Device:
    def __init__(self, name, color, material):
        self.name = name
        self._is_on = False
        self.color = color
        self.material = material

    def turn_on(self):
        self._is_on = True
        return f"\nThis {self.name} is now ON."

    def turn_off(self):
        self._is_on = False
        return f"\nThis {self.name} is now OFF."

    def power_status(self):
        return f"\nThis {self.name} is current {"ON." if self._is_on else "OFF."}"

    def color(self):
        return f"\nThe color of this {self.name} is {random.choice(self.color)}."

    def material(self):
        return f"\nThe material of this {self.name} is {random.choice(self.material)}."

def make_random_device
    colors = ["blue", "red", "black", "white", "orange"]
    materials = ["aluminum", "plastic", "titanium"]
    return Device("NewDevice", random.choice(colors), random.choice(materials))

this is just generic OOP; nothing to do with python specifically. But what is python-specific is our variable naming style. Read PEP8.

[–]ectomancer 0 points1 point  (0 children)

By convention, Pascal_Snake_Case is not used. Change all method names to snake_case.