Python Forum
How to Create Very Very Special Class with too many magic methods ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Create Very Very Special Class with too many magic methods ?
#4
>>> class example():
...     pass

>>> foo = example()

>>> foo.x = 10 # new variable

>>> foo.y = 20 # new variable

>>> foo.sum = lambda a, b: a + b 

>>> foo.sum(10, 20)
30

>>> foo.sum(foo.x, foo.y)
30

>>> foo.x = 20

>>> foo.sum(foo.x, foo.y) 
40

>>> class example2():
...     x = 5
...     y = 7

>>> bar = example2()

>>> bar.x
5

>>> bar.x = 12

>>> bar.x
12
It could be simple as this one. Why to define a method just to change a variable?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
RE: How to Create Very Very Special Class with too many magic methods ? - by wavic - Apr-13-2017, 09:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Class and methods ebn852_pan 5 291 2 hours ago
Last Post: deanhystad
  Class and methods Saida2024 2 253 May-13-2024, 04:04 AM
Last Post: deanhystad
  How does this code create a class? Pedroski55 6 609 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  Class test : good way to split methods into several files paul18fr 4 570 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  all of attributes and methods related to a special type akbarza 4 572 Jan-19-2024, 01:11 PM
Last Post: perfringo
  Structuring a large class: privite vs public methods 6hearts 3 1,160 May-05-2023, 10:06 AM
Last Post: Gribouillis
  magic related field in Django model sonh 1 1,292 Apr-24-2022, 12:37 PM
Last Post: sonh
  Cannot convert the series to <class 'int'> when trying to create new dataframe column Mark17 3 8,662 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  Need a little help with numpy array magic. pmf71 0 1,197 Dec-01-2021, 02:51 AM
Last Post: pmf71
  access share attributed among several class methods drSlump 0 1,087 Nov-18-2021, 03:02 PM
Last Post: drSlump

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020