0
import datetime
class date:
  def __init__(self,data):
    self.data=data
    x=datetime.date.today().year
    return x
y=date('data')    

I tried to show data: 'todays date:26 june' but it shows __init__() should return None, not int. What seems to be the problem here?

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • That is not a standard python error. Where, exactly, does that error come from? – John Gordon Jun 25 '23 at 16:01
  • 4
    If you don't want an object, why are you even making a class?? – John Gordon Jun 25 '23 at 16:01
  • Python already has a class `date`, please don't use the same name for your class. – Mark Ransom Jun 25 '23 at 16:15
  • 2
    it is not at all clear what you are trying to do nor why you are coding a Class that does nothing useful. What is the purpose of `self.data?` Please explain better why you need more than the basic datetime functionality as in the answer from @Mohammad. – user19077881 Jun 25 '23 at 16:31
  • You don't want to use an object in an Object Oriented Programming (OOP) environment? – DarkKnight Jun 25 '23 at 16:43
  • The answer is NO. Date and time objects may be categorized as “aware” or “naive” depending on whether or not they include timezone information. Everything is an object in python. But you have not create it by your own as in your example. – Hermann12 Jun 25 '23 at 18:38

4 Answers4

0

The __init__ function should always return None as all it does is initialise the object. Have a look here for a further explanation. A solution would be to save x as self.x and then access that variable externally or if wanted, you could modify the __new__ method.

0
import datetime
x = datetime.date.today()
print(x.strftime("Today's date: %d %b"))

This will be useful for you: How to print a date in a regular format.

Nesi
  • 286
  • 2
  • 15
  • 1
    Unfortunately, this will create datetime.date and string objects - something OP has stated clearly that she doesn't want – DarkKnight Jun 25 '23 at 16:50
0

No. There is no way to print anything in Python without creating, at the very least, a string object

DarkKnight
  • 19,739
  • 3
  • 6
  • 22
0

There is no way to do what you are trying to do in Python. To be more specific, you said that you wanted to print the date in the format "todays date:26 june" without creating an object. To do this, you have to print a string. In Python, strings are objects of the class str. So, this is not possible.