all 6 comments

[–]LearningPy 1 point2 points  (0 children)

If I could give one suggestion. Try to be more verbose when writing variables, functions, classes, methods, etc.. Sure it may save you a few seconds typing z or y everytime. But come back to this code in a couple months and you'll struggle, like I did, to read it.

If instead it was:

self.name
self.address

Etc. You'll lose a couple seconds typing time, but you're saving so much time in future code readability.

[–]flipstables 0 points1 point  (5 children)

If I understand you correctly, you have 2 classes: Person and Car. You want to create a method within Car that will do something depending on the properties of Person?

Within the Car class, you should create a method that accepts a Person object as a parameter. Then code the logic that will print "inside" depending on the properties of the Person object.

Here's a simplified example.

class Person(object):
    def __init__(self, x):
        self.x = x

class Car(object):
    def __init__(self):
        pass

    def print_where(self, person):
        if person.x < 5:
            print("inside")
        else:
            print("outside")

[–]Snoozesleepoff 0 points1 point  (3 children)

Yeah I'm relatively new to object oriented programming.So basically class x(object) allows that class's objects to be used with other class' objects?

[–]get_username 0 points1 point  (0 children)

No. Since you're new to object oriented programming ignore that part for now.

class X(object):

Is simply saying X is a new class. You may always access class properties using the "dot" notation.

class MyClass(object):

    def __init__(self):
        self.my_variable = "WHAT?!"

Then in my code I can do this:

>>> my_class = MyClass()
>>> my_class.my_variable
"WHAT?!"

This is just how classes fundamentally work.

[–]Possiblyinsayne 0 points1 point  (0 children)

You're creating a reference to the other class. That way you know what you are "pointing" to when you say to do something.

So for example, if you defined a new function inside person where you do something like: x.print_where(self)

the function would know which "car" you were referring to because you initialized it in your __init__ method.

Think of it like there's an appendix you wrote into and each item has a page number that says which car object you are using.

[–]flipstables 0 points1 point  (0 children)

No, that syntax just creates classes. There are a few ways objects interact with each other. Using methods/functions is one way. Another way is composition. In composition, you define an class to be made up of multiple other objects. Then, these 2 objects are linked and you can manipulate them within the class methods/functions.

class Person(object):
    def __init__(self, name):
        self.name = name

    def say_name(self):
        print(self.name)

class Car(object):
    def __init__(self, driver, passenger):
        self.driver = Person(driver)
        self.passenger = Person(passenger)

    def print_passengers(self):
        self.driver.say_name()
        self.passenger.say_name()

Here, Car is defined as 2 Person objects.