user777466
user777466

Reputation: 1001

Singleton when constructing an object

I have a simple class:

class Weather_data():
    def __init__(self, latitude=None, longitude=None, date=None):
        self.latitude = latitude,
        self.longitude = longitude,
        self.request_date = date

When I construct an object, I have a singleton recorded as latitude or longitude:

>>> w2=Weather_data(1.3,1.9,datetime.datetime.now())
>>> w2.latitude
(1.3,)

Why is it so?

Bonus: I have another class:

class Pricer():

    def __init__(self, realization_date=None, latitude=None, longitude=None, amount_covered=None):
        self.realization_date = realization_date
        self.latitude = latitude
        self.longitude = longitude

And when I contract an object I get a float an not a tuple:

>>> p2=Pricer(datetime.datetime.now(),1.3,1.9,100)
>>> p2.latitude
1.3

I have no idea of the difference between those 2 classes.

Upvotes: 0

Views: 62

Answers (1)

mdml
mdml

Reputation: 22882

You need to remove the commas after these lines in your first example, like so:

self.latitude = latitude
self.longitude = longitude

The commas are telling Python to create tuples that include latitude and longitude. In the second object, you don't have the commas, and thus you get floats as output.

Upvotes: 6

Related Questions